Let's beware of the emptiness
<?php $a = collect([1, 2, 3]);empty($a); // False $b = collect();empty($b); // False ##== How to check a collection for emptiness $a->isEmpty(); // False $b->isEmpty(); // True$b->count() === 0; // Truecount($b) === 0; // True
A value can be checked for emptiness. What we think make sense might, in fact, not. For example. If you check an empty Laravel collection with the PHP function "empty", you will find that it will return false. That is because, the object (of type Countable) is created, therefore the value is that object.
Using json_validate in PHP 8.3
<?php $string = '{ "version": "8.3" }'; $isValid = json_validate($string); // True //------ $invalidJson = '{ "version": "8.3 }'; // Missing closing quote $isValid = json_validate($invalidJson); // False
PHP version 8.3 introduces the method "json_validate()" with which you can make sure that a string contains valid JSON. This method is used with "json_decode()" hence you don't need to validate your string prior to decoding. If "json_validate()" returns false, the cause can be retrieved with "json_last_error()"
How to remove chart axes in a Filament Widget
<?php // In you widget fileprotected static ?array $options = [ 'scales' => [ 'x' => [ // Horizontal axis 'display' => false, ], 'y' => [ // Vertical axis 'display' => false, ], ],];
When you create a chart in Filament, sometimes you may want to hide the grid, for a doughnut chart for example. Use the static options array with the parameters above to remove the axis that you don't want.
How to get records within a quarter
<?php // Model\Payment $paymentInQ1 = Payment::whereBetween( 'due_date', [Carbon::create(2024, 1)->startOfQuarter(), Carbon::create(2024, 1)->endOfQuarter()])->get();
It is possible that in your application you have a list of Payments. You probably want to see these payment sorted by Yearly Quarters in order to create reports. For this we can use Eloquent's whereBetween, and provide the starting date and ending date of a quarter. We pass the year and the first month of the quarter manually, but you could try to calculate them dynamically.
New way to schedule tasks in Laravel 11
<?php // Laravel v11// routes/console.phpuse 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!
Globally set options for Filament components
<?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.
Route Model Binding
<?php // AncientRoute::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 youRoute::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.
Fallback Routes: Turning 404s into Opportunities
<?php// web.php or api.php Route::get('welcome'); // All your routes... // This one will trigger if no other route matchesRoute::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.