Variable region Laravel Blade @yield

I have two pages that are almost identical. One shows a list of users, the other shows the same list, but with more detailed information. Therefore, I call two views that extend the same shell. However, Laravel complains that $ user is not defined in verbose.blade.php. I pass a view to $ users that seems to be available for content.blade.php, but the $ user created in the foreach loop does not seem to be available in verbose.blade.php.

verbose.blade.php

@extends('layout.content') @section('user') {{ dd($user) }} @endsection 

nonverbose.blade.php

 @extends('layout.content') @section('user') {{ dd($user) }} @endsection 

content.blade.php

 @extends('layout.app') @section('content') @foreach($users as $user) @yield('user') @endforeach @endsection 

I also tried @yield('user', ['user' => $user])

How can I make $ user available in verbose.blade.php?

+7
laravel laravel-5 blade
source share
2 answers

Have you tried using @include?

 @include('user', ['user' => $user]) 
+2
source share

You get this error because Laravel parses click templates.

Sometimes we programmers are so rooted in our principle of β€œdon't repeat yourself (DRY)” that we go too far. This is one of those times - you should just put the foreach loop directly in verbose.blade.php:

 @extends('layout.app') @section('content') @foreach($users as $user) @yield('user') @endforeach @endsection 
0
source share

All Articles