Reputation: 99
I'm new to Laravel. In the question.blade view I receive two variables from a controller as follows :
Sometimes I want to be routed to this question.blade view but without passing one or both of the variables. I noticed if I want for example to normally be linked to this view to view its contents , I receive this error :
Undefined variable: answer (View: D:\wamp\www\xxxxxxx\resources\views\Question.blade.php)
Is there a way to tell a blade to only expect variables when the user is routed by a specific controller=>method, while not expect variables when the user is routed by another method?
I hope this is clear :)
Upvotes: 2
Views: 118
Reputation: 2755
for the part of the view which have variable data , you may separate it as a partial blade view file . then you check if it is exists. or count the array .
@if(count($array))
<div class="col-xs-12" style="margin: auto;">
@include('partials.myview')
</div>
@endif
Better to give the variable or array empty value to avoid errors for whatever reason .
Upvotes: 0
Reputation: 5779
Simple way is to just check if desired variable does exist:
@if (isset($answers))
@foreach ($answers as $answer)
...
@endforeach
@endif
{{ isset($post) ? $post->body : '' }}
Upvotes: 2