A set of handy utilities for any Laravel project.
- PHP ^8.4
- Laravel ^12.0
You can install the package via Composer:
composer require sebastiansulinski/laravel-bitsThe package will automatically register its service provider.
The EnhancedEnums trait provides additional utility methods for PHP enums, making them more convenient to work with in
Laravel applications.
use LaravelBits\Traits\EnhancedEnums;
enum Status: string
{
use EnhancedEnums;
case Active = 'active';
case Pending = 'pending';
case Closed = 'closed';
public function label(): string
{
return ucfirst($this->value) . ' Status';
}
}values(?array $cases = null): array- Get array of enum valuesnames(?array $cases = null): array- Get array of enum namesoptions(?array $cases = null): array- Get array of name/value pairstoArray(): array- Convert enum instance to arraylabel(): string- Get label for enum case (can be overridden)except(array|self $cases): array- Exclude certain cases from enumcollection(): Collection- Get cases as Laravel Collection
// Get all values
Status::values(); // ['active', 'pending', 'closed']
// Get all names
Status::names(); // ['Active', 'Pending', 'Closed']
// Get options for dropdowns
Status::options();
// [
// ['name' => 'Active Status', 'value' => 'active'],
// ['name' => 'Pending Status', 'value' => 'pending'],
// ['name' => 'Closed Status', 'value' => 'closed']
// ]
// Convert single case to array
Status::Active->toArray(); // ['name' => 'Active Status', 'value' => 'active']
// Exclude certain cases
Status::except([Status::Active, Status::Closed]); // Only Pending case
// Get as Collection
Status::collection(); // Collection of all casesThe Serialisable trait provides JSON serialization capabilities for any class.
use LaravelBits\Traits\Serialisable;
class MyClass
{
use Serialisable;
public function toArray(): array
{
return [
'property1' => $this->property1,
'property2' => $this->property2,
];
}
}__toString(): string- Convert object to JSON stringtoJson($options = 0): string- Get JSON representation of the objecttoArray(): array- Abstract method that must be implemented
$object = new MyClass();
// Convert to JSON string
echo $object; // Calls __toString() which returns JSON
// Get JSON with options
$json = $object->toJson(JSON_PRETTY_PRINT);The Sortable trait provides utilities for handling sortable records with automatic sort position management.
use LaravelBits\Traits\Sortable;
class MySortableService
{
use Sortable;
public function sort(array $ids): void
{
MyModel::upsert(
$this->sortablePayload($ids), 'ulid', ['sort']
);
}
protected function existingSortables(array $ids): Collection
{
return MyModel::whereIn('ulid', $ids)->get();
}
protected function sortableModelColumns(): array
{
return ['ulid', 'name', 'description'];
}
}The Sorter and SorterPayload classes provide a powerful way to reorder records based on a new sequence of IDs while
maintaining proper sort values. This is particularly useful for drag-and-drop interfaces or any scenario where you need
to update the sort order of multiple records efficiently.
The SorterPayload class prepares a collection of models in the desired order based on an array of IDs.
Constructor Parameters:
$models- Collection of Eloquent models to be reordered$ids- Array of IDs in the desired order$filter- String column name or Closure to match models with IDs (defaults to 'id')
The Sorter class generates update sets for bulk database operations based on the reordered payload.
Constructor Parameters:
$payload- SorterPayload instance with reordered records$idColumn- Column name used as the primary key (defaults to 'id'). When used in combination with EloquentBuilder::updateMany(), this should match the first argument$caseColumn.$sortColumn- Column name used for sorting (defaults to 'sort')$updateColumn- Column name to update (defaults to 'sort')
Methods:
getSet(?Closure $callback = null): UpdateManySet- Generate an UpdateManySet for bulk updates
Basic Sorting:
use LaravelBits\Utilities\Sorter\Sorter;
use LaravelBits\Utilities\Sorter\SorterPayload;
// Get your models
$models = Book::where('category_id', 1)->get();
// Define the new order using IDs
$newOrder = [3, 1, 4, 2, 5]; // Book IDs in desired order
// Create the payload
$payload = new SorterPayload(
models: $models,
ids: $newOrder,
filter: 'id' // Match by 'id' column
);
// Create the sorter
$sorter = new Sorter(payload: $payload);
// Get the update set and apply it
$updateSet = $sorter->getSet();
Book::updateMany('id', $updateSet);Advanced Sorting with Custom Columns:
// Using ULID as identifier and custom sort column
$models = Product::where('active', true)->get();
$ulidOrder = ['01H123...', '01H456...', '01H789...']; // ULIDs in desired order
$payload = new SorterPayload(
models: $models,
ids: $ulidOrder,
filter: fn($model, $ulid) => $model->ulid->toString() === $ulid
);
$sorter = new Sorter(
payload: $payload,
idColumn: 'id',
sortColumn: 'position',
updateColumn: 'position'
);
$updateSet = $sorter->getSet();
Product::updateMany('id', $updateSet);Custom Value Generation:
// Generate custom values with a callback
$sorter = new Sorter(
payload: $payload,
updateColumn: 'display_name'
);
$updateSet = $sorter->getSet(
fn($model, $index, $sort) => "Item #{$index} (Sort: {$sort})"
);
Product::updateMany('id', $updateSet);The callback function receives three parameters:
$model- The current Eloquent model$index- Zero-based index in the reordered sequence$sort- The calculated sort value for this position
Adds a whereDateBetween method to Laravel's Query Builder for filtering records between two dates.
use Illuminate\Support\Facades\DB;
// Filter records between two dates
DB::table('orders')
->whereDateBetween('created_at', ['2024-01-01', '2024-12-31'])
->get();
// With custom boolean operator
DB::table('orders')
->where('status', 'active')
->whereDateBetween('created_at', ['2024-01-01', '2024-12-31'], 'or')
->get();$column- The column name to filter$dates- Array with two dates[start_date, end_date]$boolean- Boolean operator ('and' or 'or'), defaults to 'and'
Adds an updateMany method to Laravel's Eloquent Builder for efficiently updating multiple records with different
values in a single query.
use App\Models\Book;
use LaravelBits\Data\UpdateManySet;
// Update multiple records with different sort values
Book::updateMany('id', new UpdateManySet('sort', [
1 => 30,
2 => 20,
3 => 10,
]));
// Update multiple columns at once
Book::updateMany('id', [
new UpdateManySet('sort', [1 => 30, 2 => 20, 3 => 10]),
new UpdateManySet('priority', [1 => 'high', 2 => 'medium', 3 => 'low']),
]);
// With custom timestamp
Book::updateMany('id', new UpdateManySet('sort', [
1 => 30,
2 => 20,
3 => 10,
]), now()->addHour());$caseColumn- The column to match against (usually 'id')$sets- UpdateManySet instance or array of UpdateManySet instances$timestamp- Optional timestamp for updated_at (defaults to now())
- Performance: Updates multiple records in a single SQL query using CASE statements
- Efficiency: Avoids N+1 query problems when updating many records
- Automatic timestamps: Automatically updates the
updated_atcolumn
This package is open-sourced software licensed under the MIT license.
Please see CONTRIBUTING for details.