This is the next iteration to the Laravel Snippets Series.
Here are 10 more Laravel snippets/methods I use or have recently saved for later:
1. Reorder orderBy()
If you try to use orderBy
on a query that has already been ordered, it will not work since in SQL you can have multiple order by's in a query, so they just end up getting appended.
If you explicitly want to change the ordering, you can use reorder:
// Consider a relationship
public function posts()
{
return $this->hasMany(Post::class)->latest();
}
// The above already sets the orderBy to `created_at desc` so the following will not override that.
$user->posts()->orderBy('updated_at', 'desc')->get(); // Still ordered by `created_at desc`
// Reorder to a different column
$user->posts()->reorder()->orderBy('updated_at', 'desc')->get(); // Now ordered by `updated_at desc`
// Or more cleaner
$user->posts()->reorder('updated_at', 'desc')->get();
2. isDirty()
Check if a model has been modified
$product = Product::find(1);
$product->isDirty() // false
$product->name = "New name";
$product->isDirty() // true
$product->isDirty('name') // true
3. @checked
Clean up your markup with the new @checked directive.
From:
<input type="radio" name="active" value="1" {{ old('active', user->active)) ? 'checked' : '' }} />
To:
<input type="radio" name="active" value="1" @checked(old('active', user->active)) />
4. @selected
Clean up your markup with the new @selected directive.
<select name="active">
<option value="yes" {{ old('active', user->active)) ? 'selected' : '' }}>Yes</option>
</select>
To:
<select name="active">
<option value="yes" @selected(old('active', user->active))>Yes</option>
</select>
5. foreignIdFor()
Instead of using foreignId
with a string column, you can specify a model.
$table->foreignIdFor(\App\Models\Author::class);
6. Route::controller
Laravel 8.80.0 release includes support for defining a common group controller once for a route group, then only providing the methods to the route definitions.
Route::controller(OrderController::class)->group(function () {
Route::get('/orders/{id}', 'show');
Route::post('/orders', 'store');
});
7. make:migration strings
From:
php artisan make:migration create_users_table
To:
php artisan make:migration "create users table"
8. Mail::alwaysTo()
Avoiding Accidental Email Sends with alwaysTo()
// AppServiceProvider.php
if (! app()->environment('production')) {
Mail::alwaysTo('[email protected]');
}
9. str()
The str
function returns a new Illuminate\Support\Stringable
instance of the given string. This function is equivalent to the Str::of
method:
$string = str('Taylor')->append(' Otwell');
// 'Taylor Otwell'
10. skip() TransformRequests-type middleware
As of @laravelphp v8.36.0, you can skip 'TransformRequests' middlewares by registering a callback.
For example, you can use it on 'TrimStrings' and 'ConvertEmptyStringsToNull'
ConvertEmptyStringsToNull::skipWhen(
fn (Request $request) => $request->is('admin/*')
);
TrimStrings::skipWhen(
fn (Request $request) => $request->is('admin/*')
);