Laravel Call Function Controller in Blade

Sovary September 17, 2022 803
2 minutes read

Today I will show step by step how to access function in controller from blade Laravel. The below example will give you idea show simple example of Laravel call controller function from HTML blade. You can use this article to implement and call function in blade Laravel and apply with Laravel 6, Laravel 7, Laravel 8 and Laravel 9.

We will call controller function from blade file with 2 ways. We will call controller static function from view directly. Another example will use helper functions to call directly in blade file. I will show you below example

Laravel Call Function Controller in Blade

  1. Using Custom Helper Function
  2. Using Controller Static Method

1. Using Custom Helper Function

You will have to create a file under app directory which is in your laravel project and place below code in file

Create file app -> Helper -> helpers.php

<?php

if (!function_exists('countSpace')) 
{
    function countSpace($txt)
    {
        return count(explode(" ",$txt));
    }
}

Then you have to tell composer where your helper file is. So open composer.json file and put following code

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    },
    "files": [
        "app/Helpers/helpers.php"
    ]
},

After that, we have to run composer auto load command so that will load our helper file.

composer dump-autoload

Now you can use the function in blade file as below example:

Open file resources -> views -> welcome.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Example Call Controller Function in Blade - CamboTutorial.com</title>
</head>
  
<body>
    <p>{{ countSpace("Welcome to cambotutorial for free learning website.") }}</p>
</body>
  
</html>

2. Using Controller Static Method

We will create a controller and implement static function with any logic in the body function.

Create file app -> Http -> Controllers -> PostController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class PostController extends Controller
{

    public function index()
    { 
        return view('index');
    }
  
    
    public static function countSpace($txt)
    {
        return count(explode(" ",$txt));
    }
}

Then you can call the static function in blade file as below

<!DOCTYPE html>
<html>
<head>
    <title>Example Call Controller Function in Blade - CamboTutorial.com</title>
</head>
  
@php
    $val= App\Http\Controllers\PostController::countSpace("Welcome to cambotutorial");
@endphp
  
<body>
    <p>There are {{ $val }} spaces</p>
</body>
  
</html>

Hope this short article help you to complete your project. Thanks

You may also like

 

Laravel  PHP 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him