Larahints

Hints to help you on your Laravel journey.

New way to schedule tasks in Laravel 11

Laravel v11
                    
<?php
 
// Laravel v11
// routes/console.php
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schedule;
 
Schedule::call(function () {
DB::table('recent_users')->delete();
})->daily();
 
// Laravel v10
// Same code would have been in app/Console/Kernel.php, in the schedule method
 
// Bonus:
// See a list of your schedule tasks in the console with:
// php artisan schedule:list

Laravel v11 brings a change in the way you declare your scheduled tasks. From now on, you can call your tasks within routes/console.php. Simple!

Admin
Admin
1 month ago

Globally set options for Filament components

Filament v3
                    
<?php
 
use Filament\Tables\Actions\CreateAction;
 
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
CreateAction::configureUsing(modifyUsing: function (CreateAction $action) {
// Here you can set any configuration you need for all Table CreateAction
// Of course, you can override these settings on each Action
 
// All actions will use the slideOver function
$action->slideOver();
});
}
}

Most Filament components (Table, Action, Notification, Infolist...) can be globally configured. This reduces repetition and allow for a more streamlined approach. Of course, each option can later be overridden on each component. All you have to do is setup these option in the boot method of any Service Provider.

Admin
Admin
4 months ago

Route Model Binding

Laravel v10
                    
<?php
 
// Ancient
Route::get('/users/{id}', function ($id) {
$user = User::find($id);
return $user->email;
});
 
// Modern - There is no need to fetch the user, Laravel does it for you
Route::get('/users/{user}', function (App\Models\User $user) {
return $user->email;
});

Automatically retrieve model instances from route parameters, eliminating manual lookups. No need for `Model::find($id)` anymore. Laravel will automagically fetch the proper record so long as you type hint the Model in the function's argument.

Admin
Admin
4 months ago

Fallback Routes: Turning 404s into Opportunities

Laravel v10
                    
<?php
// web.php or api.php
 
Route::get('welcome'); // All your routes...
 
// This one will trigger if no other route matches
Route::fallback(function () {
return view('404');
});

This handy method lets you define a special route that gets triggered when no other route fits the bill. Instead of the usual "404" screen, your app can offer a custom experience for any lost souls wandering your digital domain. Think of it as a safety net for your routes. All existing middleware from the "web" group in your routes/web.php file will apply to this fallback route, so your app's security and functionality still hold strong. And if you need extra control, you can always add additional middleware just for this route, tailoring it to handle any unexpected situations with finesse.

Admin
Admin
4 months ago