Reputation: 290
I'm new to Laravel blade and trying to nest an if
statement inside a foreach
loop. I want to show a different link depending on whether a field in my table equals Submitted
. I have written the following code in my view:
@foreach($plans as $plan)
<tr>
<td> {{$plan->id}}</td>
{{-- If the Plan Submission has been submitted, the link should bring the user to the Show function which is view only. Cannot have user editing Plan Submission after it has been submitted. --}}
@if ({{$plan->status}}=='Submitted')
<td><a href="/basicinfo/{{$plan->id}}/show">Click Here</a></td>
@else
<td><a href="/basicinfo/{{$plan->id}}/edit">Click Here</a></td>
@endif
I am getting this error: syntax error
unexpected '<'
Upvotes: 0
Views: 1406
Reputation:
This should work...
@foreach($plans as $plan)
<tr>
<td>{{ $plan->id }}</td>
@if ($plan->status === 'Submitted')
<td><a href="/basicinfo/{{ $plan->id }}/show">Click Here</a></td>
@else
<td><a href="/basicinfo/{{$plan->id}}/edit">Click Here</a></td>
@endif
</tr>
@endforeach
Upvotes: 0
Reputation: 1404
The error is due to a syntax error.
{{$plan->status}}=='Submitted'
Inside if, you don't need to use {{}}
as you are already writing PHP script using inline @if
. We used {{ }}
when we want to print a PHP variable within the HTML. You can read about Laravel blade for more details.
Just replace {{$plan->status}}=='Submitted'
with $plan->status == 'Submitted'
.
if
statement.@foreach($plans as $plan)
@php
$url = "/basicinfo/{$plan->id}/";
$url .= strtolower($plan->status) =='submitted' ? 'show' : 'edit';
@endphp
<tr>
<td> {{$plan->id}}</td>
<td><a href={{$url}}>Click Here</a></td>
</tr>
@endforeach;
NOTE: I've used strtolower() php function for better comparison.
Upvotes: 0
Reputation: 426
Try this
@foreach($plans as $plan)
<tr>
<td> {{$plan->id}}</td>
<td>
<a href="/basicinfo/{{$plan->id}}/@if($plan->id=='submitted') show
@else edit @endif">
Click Here
</a>
</td>
</tr>
@endforeach
Upvotes: 0
Reputation: 4275
Change to:
@foreach($plans as $plan)
<tr>
<td> {{$plan->id}}</td>
{{-- If the Plan Submission has been submitted, the link should bring the user to the Show function which is view only. Cannot have user editing Plan Submission after it has been submitted. --}}
@if ($plan->status == 'Submitted')
<td><a href="/basicinfo/{{$plan->id}}/show">Click Here</a></td>
@else
<td><a href="/basicinfo/{{$plan->id}}/edit">Click Here</a></td>
@endif
It was @if ({{$plan->status}}=='Submitted')
No need for the {{ }}
inside the if statement :)
So the change in total was:
@if ({{$plan->status}}=='Submitted')
to @if ($plan->status == 'Submitted')
Upvotes: 2