Awesome
<picture> <source media="(prefers-color-scheme: dark)" srcset="https://banners.beyondco.de/Laravel%20Extended.png?theme=dark&packageManager=composer+require&packageName=onursimsek%2Flaravel-extended&pattern=topography&style=style_1&description=Extend+your+Laravel+project+with+mixins+and+mores&md=1&showWatermark=0&fontSize=100px&images=arrows-expand"> <source media="(prefers-color-scheme: light)" srcset="https://banners.beyondco.de/Laravel%20Extended.png?theme=light&packageManager=composer+require&packageName=onursimsek%2Flaravel-extended&pattern=topography&style=style_1&description=Extend+your+Laravel+project+with+mixins+and+mores&md=1&showWatermark=0&fontSize=100px&images=arrows-expand"> <img alt="Package Image" src="https://banners.beyondco.de/Precondition.png?theme=dark&packageManager=composer+require&packageName=onursimsek%2Fprecondition&pattern=topography&style=style_2&description=HTTP+Precondition+for+Laravel&md=1&showWatermark=1&fontSize=125px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg"> </picture>Extend your Laravel project with mixins and mores
Installation
You can install the package via composer:
composer require onursimsek/laravel-extended
Contents
- Illuminate\Database\Query\Builder
- Illuminate\Support\Str
- Illuminate\Support\Stringable
- Useful Traits
Usage
Extended Illuminate\Database\Query\Builder
Product::whereGreaterThan('price', 500)->get();
// select * from products where price > 500
Product::whereGreaterThanOrEqual('price', 500)->get();
// select * from products where price >= 500
Product::whereLessThan('price', 500)->get();
// select * from products where price < 500
Product::whereLessThanOrEqual('price', 500)->get();
// select * from products where price <= 500
Product::whereColumnGreaterThan('price', 'amount')->get();
// select * from products where price > amount
Product::whereColumnGreaterThanOrEqual('price', 'amount')->get();
// select * from products where price >= amount
Product::whereColumnLessThan('price', 'amount')->get();
// select * from products where price < amount
Product::whereColumnLessThanOrEqual('price', 'amount')->get();
// select * from products where price <= amount
Product::whenWhere(false, 'is_active')->get();
// select * from products
Product::whenWhere(true, 'is_active')->get();
// select * from products where is_active = 1
Extended Illuminate\Support\Str
use Illuminate\Support\Str;
Str::squishBetween("I\twill kiss\t\nyou!", 'kiss', 'you');
// I will kiss you!
Str::replaceBetween('I will kiss you!', 'will', 'you', 'miss');
// I will miss you!
Str::replaceBetweenMatch('I will kiss you!', 'will', 'you', '/k(.*)s/', 'hug');
// I will hug you!
Extended Illuminate\Support\Stringable
use Illuminate\Support\Str;
Str::of("I\twill kiss\t\nyou!")->squishBetween('kiss', 'you');
// I will kiss you!
Str::of('I will kiss you!')->replaceBetween('will', 'you', 'miss');
// I will miss you!
Str::of('I will kiss you!')->replaceBetweenMatch('will', 'you', '/k(.*)s/', 'hug');
// I will hug you!
Useful Traits
InteractsWithDatabase
This trait provides an easy way to manage database transactions across multiple connections. It allows you to begin, commit, and roll back transactions.
beginTransaction(string ...$connections): void
This method starts a transaction on the specified database connections. If no connections are provided, the default database connection specified in your Laravel configuration will be used.
$this->beginTransaction(); // Starts transaction on default connection
$this->beginTransaction('mysql', 'sqlite'); // Starts transactions on the 'mysql' and 'sqlite' connections
commit(string ...$connections): void
This method commits a transaction on the specified connections. If no connections are specified, nothing will happen.
$this->commit(); // No action taken (no specific connection provided)
$this->commit('mysql', 'sqlite'); // Commits the transactions on the 'mysql' and 'sqlite' connections
commitAll(): void
This method commits transactions on all connections that have begun a transaction during the lifetime of the object.
$this->commitAll(); // Commits all active transactions
rollBack(string ...$connections): void
This method rolls back a transaction on the specified connections.
$this->rollBack(); // Rolls back on the default connection
$this->rollBack('mysql', 'sqlite'); // Rolls back on the 'mysql' and 'sqlite' connections
rollBackAll(): void
This method rolls back transactions on all connections that have begun a transaction during the lifetime of the object.
$this->rollBackAll(); // Rolls back all active transactions
InteractsWithDatabase Example
namespace App\Http\Controllers\Controller;
use OnurSimsek\LaravelExtended\Support\InteractsWithDatabase;
class Controller
{
use InteractsWithDatabase;
public function store()
{
$this->beginTransaction('mysql', 'pgsql');
try {
// Your data processing logic
$this->commitAll(); // Commit the transactions if everything goes well
} catch (\Exception $e) {
$this->rollBackAll(); // Roll back if an error occurs
throw $e;
}
}
}
HasName
This trait converts the names of UnitEnum into an array.
names()
enum Status
{
use HasName;
case Active;
case Inactive;
}
Status::names(); // ['Active', 'Inactive']
HasValue
values() and names()
This trait converts the names and values of BackedEnum into an array
enum Status: string
{
use HasValue;
case Active = 'active';
case Inactive = 'inactive';
}
Status::names(); // ['Active', 'Inactive']
Status::values(); // ['active', 'inactive']
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.