0

I am trying to do a if else statement with a foreach loop but for some reason I am getting this error

Parse error: syntax error, unexpected 'elseif' (T_ELSEIF).

I know that this is a simple error but I can't seem to identify what is wrong here, did I do my code wrongly? Or is it something that need to be closed? (for the closing part, I have checked and I don't really see any problem with it) Can someone help me? Thanks a lot

profile.blade.php

@foreach($data as $value) 
  <tr> 
 @if($value->training_Status == 'No')
 <th><a href="{{route('user.show',['id'=>$value->pi_id])}}"><span style="color: blue;">{{$value->Name}}</span></a></th>
 @foreach($data1 as $value1)
  @elseif(($value1->blacklist == 'Yes') && ($value1->name == $value->Name))
 <th><a href="{{route('user.show',['id'=>$value->pi_id])}}"><span style="color: red;">{{$value->Name}}</span></a></th>
  @endforeach
 @else 
<th><a href="{{route('user.show',['id'=>$value->pi_id])}}">{{$value->Name}}</a></th>
 @endif 
 </tr> 
 @endforeach
2
  • 3
    @foreach and @endforeach are in two completely different code-blocks. Commented Dec 1, 2017 at 6:11
  • You mean the foreach I have put the place wrongly? Commented Dec 1, 2017 at 6:12

3 Answers 3

1

You can change to

@foreach($data as $value) 
  <tr> 
 @if($value->training_Status == 'No')
 <th><a href="{{route('user.show',['id'=>$value->pi_id])}}"><span style="color: blue;">{{$value->Name}}</span></a></th>
 @foreach($data1 as $value1)
  @if(($value1->blacklist == 'Yes') && ($value1->name == $value->Name))
 <th><a href="{{route('user.show',['id'=>$value->pi_id])}}"><span style="color: red;">{{$value->Name}}</span></a></th>
     @endif 
  @endforeach
 @else 
<th><a href="{{route('user.show',['id'=>$value->pi_id])}}">{{$value->Name}}</a></th>
 @endif 
 </tr> 
 @endforeach
Sign up to request clarification or add additional context in comments.

4 Comments

Ohh it was the problem with endif but in the past when I didn't put in the second foreach, the @endif just need to put it 1 time, why do I need to put it twice now?
because the second foreach contains an if ... before there was 1 if so 1 endif ... now there are 2 ifs, so 2 endifs ... if there was 3 ifs there would be 3 endifs ... if there was 4 ...
But can't we just use elseif instead of the seond if? Why do I need to use another if instead?
because its not part of the previous conditionals, its a new logical condition at that point in the code in this current block .. there is a structure for the language and logic, its fundamental
1
@foreach($data as $value) 
 <tr> 
 @if($value->training_Status == 'No')
   <th><a href="{{route('user.show',['id'=>$value->pi_id])}}"><span style="color: blue;">{{$value->Name}}</span></a></th>
   @foreach($data1 as $value1)
     @if(($value1->blacklist == 'Yes') && ($value1->name == $value->Name))
     <th><a href="{{route('user.show',['id'=>$value->pi_id])}}"><span style="color: red;">{{$value->Name}}</span></a></th>
     @endif 
   @endforeach
 @else 
   <th><a href="{{route('user.show',['id'=>$value->pi_id])}}">{{$value->Name}}</a></th>
 @endif 
 </tr> 
 @endforeach

Should do the job for you

Comments

1

@Dkna Just change your code to like this:

@foreach($data as $value) 
    <tr> 
    @if($value->training_Status == 'No')
       <th><a href="{{route('user.show',['id'=>$value->pi_id])}}"><span style="color: blue;">{{$value->Name}}</span></a></th>
       @foreach($data1 as $value1)
           @if(($value1->blacklist == 'Yes') && ($value1->name == $value->Name))
              <th><a href="{{route('user.show',['id'=>$value->pi_id])}}"><span style="color: red;">{{$value->Name}}</span></a></th>
           @endif 
       @endforeach
    @else 
        <th><a href="{{route('user.show',['id'=>$value->pi_id])}}">{{$value->Name}}</a></th>
    @endif 
    </tr> 
@endforeach

Hope this helps you and fixed your issue!

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.