Search code examples
phplaravellaravel-5laravel-5.3

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


Solution

  • 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'].