0

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 '<'

4 Answers 4

2

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')

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Solution

Just replace {{$plan->status}}=='Submitted' with $plan->status == 'Submitted'.


Another way to implement the same just by using inline 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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.