Conditionally executing callbacks using Conditionable trait in Laravel 8.x

1 · Amit Merchant · June 6, 2021, 7:21 p.m.
If you have been working with Laravel lately, you might have used this feature called “conditional clauses” using which you will be able to run a certain callback when a condition becomes true. For instance, take the example of Laravel’s query builder where using the when method, you can query clauses to apply to a query based on another condition like so. $isAdmin = $request->input('is_admin'); $books = DB::table('books') ->when($isAdmin, function ($query, $role) { ...