How to make dynamic @yield in laravel?

Member

by lew , in category: PHP Frameworks , 23 days ago

How to make dynamic @yield in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by aubrey , 22 days ago

@lew 

To create a dynamic @yield in Laravel, you can pass a variable to the yield directive in your blade template. Here's an example of how you can achieve this:

  1. Define a variable in your controller method:
1
2
3
4
$data = [
    'content' => 'This is some dynamic content'
];
return view('your-view')->with($data);


  1. Pass this variable to the yield directive in your blade template:
1
@yield('content', 'Default content')


  1. In your child view, use the @section directive to define the content to be yielded:
1
2
3
@section('content')
    {{ $content }}
@endsection


This way, the variable content will be dynamically passed to the yield directive in your blade template and displayed in the child view. If the variable is not passed, the default content specified in the @yield directive will be displayed instead.