Laravel Facades

Architecture Concepts LaravelLeave a Comment on Laravel Facades

Laravel Facades

Getting non static function statically is called Facades.

Lets set the cache like below in our web route

cache()->set('name',"navid");

and get it using dd()

dd(cache()->get('name'));
Result : “navid”

As we can see cache() is not a static method. If we Ctrl+Left Mouse Click on cache(),it will go to that method and we can see the cache()

function cache()
    {
        $arguments = func_get_args();

        if (empty($arguments)) {
            return app('cache');
        }

        if (is_string($arguments[0])) {
            return app('cache')->get(...$arguments);
        }

        if (! is_array($arguments[0])) {
            throw new Exception(
                'When setting a value in the cache, you must pass an array of key / value pairs.'
            );
        }

        return app('cache')->put(key($arguments[0]), reset($arguments[0]), $arguments[1] ?? null);
    }
}

Though cache() is not a static function ,but in Laravel we can call it statically .It is called Facades.

use Illuminate\Support\Facades\Cache; cache::set('name',"navid");
dd(cache::get('name')); 

Result is “navid”

Check out the official documentation of Facades

https://laravel.com/docs/8.x/facades

Full-stack web developer and founder of Laravelaura. He makes his tutorials as simple as humanly possible and focuses on getting the students to the point where they can build projects independently. https://github.com/NavidAnjum

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top