Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

danilotrix's avatar

Calling Controller functions in blade tempalte

Hello guys, I'm new to Laravel and I'm trying to call a Controller function from a blade template. I'm doing something definitely wrong.

Controller

class MenuController extends Controller
{
    public function getMenu(){
        dd(Prompt::all('service'));
    }
}

Blade template

{{ action([\App\Http\Controllers\MenuController::class, 'getMenu']) }}
0 likes
19 replies
Sinnbeck's avatar

You are meant to pass data down. Not call controllers from a blade component. So send that data inside the component or pass it in as data

danilotrix's avatar

@Sinnbeck thanks for the asnwer. can you explain a bit better? it's not clear to me what you're saying.

My idea is to dynamically create the menu with items from the database.

martinbean's avatar

@danilotrix You don’t call controllers from views. You pass data from controllers, to views.

You’d probably be better off actually watching some lessons on this site to get a handle on fundamentals like this.

danilotrix's avatar

ok guys I understood how to do it thanks. However, I still have some issue

in my Route I use this

Route::get('/prompts/{cat}/{ser}', [DashboardController::class, 'index'])
    ->middleware(['auth', 'verified']);

in DashboardController

public function index()
    {
      	$menu_from_service = new MenuController;
        $menu = $menu_from_service->getMenu();
        return view('pages.dashboards.index', $menu);
    }

the problem is that, this method returns a blade template which is included in the master template. and $menu does not have scope in the master, where the menu should be.

hope it;s clear

Sinnbeck's avatar

@danilotrix ok maybe I wasn't quite clear. You don't call a controller from another controller. You can move that logic to a model, a service or even use an action. Hard to give a best solution as I don't know the code. But a controller is meant to be called from a http request and return a response. Simple as that

danilotrix's avatar

@Sinnbeck ok I see.

The point here is another.

return view('pages.dashboards.index', $menu);

but index is a blade template which is included by the master template. and the master template does not see @menu.

martinbean's avatar

@danilotrix Can you please watch some lessons on this site. Then you won’t keep making fundamental mistakes as above.

martinbean's avatar

@danilotrix No, because Laracasts is more authoritative for learning Laravel versus random videos on YouTube that may be out of date or just plain incorrect.

danilotrix's avatar

@martinbean thank you very much for your advice. actually didn't know laracast had lessons. I watched videos from developers that work at Laravel so I guess they were very accurate too.

hendro-dev's avatar

Not everything should be made from controller, cuz it's not effective if you want to show that data for every page. The solution is, you can make your own helpers function like this

laravel-news . com/creating-helpers

You can use it on Controllers, Models even Blades

hendro-dev's avatar

@Snapey I figure out using view composer is a bad practice, it will make laravel calling multiple query for every included blade. I think a custom helpers is still the best option.

Please or to participate in this conversation.