Using controller function directly in Laravel blade

Today I would like to talk about using the function from controller directly on your blade view files.

Normally we wrote codes relating with application logic and database and then pass the data to specific view files. But what if we like to use the function from controller directly ?

We can create a static function in controller and then can directly be called from blade file without instantiating the class. Let me write a sample example.

Firstly, we will create a static function in controller. Why static function ?

If we create a normal function, we will need to instantiate the class in view file like this new Controller() which will be a mess in view files. Static function can be directly called using scope resolution (::) operator.

After that we can call the above controller function directly by providing the namespace of the controller and function using scope resolution operator like this.

By Yuuma




tel. 06-6454-8833(平日 10:00~17:00)