问题
Hey as i am passing a blade view which is having it own controller also i am including it into the view which does not have its own controller. it gives me an undefined variable error can any one help me how to it.
I have a view which does not have any controller only have Route like this Route::get('index', function () { return view('index'); });
in this view i am passing another view which having its own controller and also having some data from an array. but after using this view inside the view i get undefined variable error.
回答1:
two steps.
Declare & transfer $variable to View from Controller function.
public function index() { return view("index", [ "variable" => $variable ]); }
Indicate where transferred $variable from Controller appear in view.blade.php.
{{ $variable }}
if you don't make sure, $variable is transferred or not
{{ isset($variable) ? $variable : '' }}
回答2:
You can try this:
public function indexYourViews()
{
$test = "Test Views";
$secondViews = view('second',compact('test'));
return view('firstview',compact('secondViews'));
}
and after declare {{$secondViews}}
in your main view file(firstview).
Hope this helps you.
回答3:
public function returnTwoViews() {
$variable = 'foo bar';
$innerView = view('inner.view', ['variable' => $variable]);
return view('wrapper.view, ['innerView' => $innerView]);
}
This may be what you are looking for?
... inside your wrapper.view template:
{!! $innerView !!}
EDIT: to answer the question in the comment: In order to fetch each line you for do this inside your $innerView
view:
@foreach($variable as $item)
{{ $item }}
@endforeach
... and in the wrapper view it will still be {!! $innerView !!}
来源:https://stackoverflow.com/questions/39789211/variable-undefined-error-in-laravel-blade-view