Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we store value of @yield in a variable

I have a simple question can we store the value that will be yielded in a variable.

for instance an example

$var = @yield('title')

If no, then is there any way to get the value of this yield

like image 951
Alen Avatar asked Sep 15 '25 08:09

Alen


1 Answers

Yes you can retrieve the value of an yield which is defined by a section using something like the following, for example:

// Assumed you've the following in your view: @section('title', 'Some Title')

$title = app()->view->getSections()['title']; // Some Title

Basically, the app()->view->getSections() will return you an associative array of all the sections so, to get a specific section, all you need to get an specific index from the array. From the view, you can access the app using the global $app variable, i.e: $app->view or $app['view'].

like image 52
The Alpha Avatar answered Sep 17 '25 22:09

The Alpha