Home > Enterprise >  If condition inside foreach loop in Laravel
If condition inside foreach loop in Laravel

Time:03-21

I'm new to laravel. How can i put if condition inside the foreach loop in blade template

my code is:

enter code here@foreach ($data as $data)
question: {{ $data->qid }}
Your answer: {{ $data->answer }}
Correct answer: {{ $data->anskey }}

@endforeach

CodePudding user response:

@foreach($data as $data)
    Question: {{ $data->qid }}
    Your answer: {{ $data->answer }}
    @if($data->anskey === $data->answer)
        You correct
    @else
        You incorrect
    @endif
@endforeach

CodePudding user response:

####Ols
@foreach ($users as $user)
    <p>This is user {{ $user->id }}</p>
@endforeach

####new syntix for and if
@forelse ($users as $user)
    <li>{{ $user->name }}</li>
@empty
    <p>No users</p>
@endforelse
  • Related