Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Conditionally executing callbacks using Conditionable trait in Laravel 8.x

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) {
                    return $query->where('is_pulished', 1);
                })
                ->get();

As you can tell, the when method accepts two arguments here. The first argument, which would typically be a boolean value, when it’s true, the second argument which is a callback will get executed. If it is false, it won’t execute the closure.

There’s another method called unless(the opposite of the when method) which will execute the given callback unless the first argument given to the method evaluates to true.

The similar is the case with Laravel collections as well. You can use the when and unless methods on collections the similar way I have described above.

These methods are really simple but make the code concise and more readable.

So, what if you want to use a similar kind of convenience in your own custom classes? Let’s say, in your services?

Well, there’s now a workaround for the same in Laravel 8.x with the introduction of the Conditionable trait.

The Conditionable trait for custom classes

Laravel 8.x now comes packed with an Illuminate\Support\Traits\Conditionable trait which you can use in your classes to add when and unless methods in your custom classes.

Let’s say, if you have a BookService class and you want to use the Conditionable trait in it, you can do it like so.

use Illuminate\Support\Traits\Conditionable;

class BookService
{
    use Conditionable;

    function fetchCategories() {
        //
    }

    function fetchAuthors() {
        //
    }
}

Once added, here’s you can make use of when and unless methods like so.

$bookService = new BookService;

$bookService = $bookService->when(true, function ($bookService) {
    return $bookService->fetchCategories();
})->fetchAuthors();

$bookService = $bookService->unless(true, function ($bookService) {
    return $bookService->fetchCategories();
})->fetchAuthors();
Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?