Reputation: 43
Hi I'm trying to show/hide select options in blade view in Laravel.
im passing in the Model ($assessments) to the view:
[{"id":1,"user_id":1,"name":"Appointment 1","created_at":"2018-03-31 00:00:00","updated_at":"2018-03-31 00:00:00"},{"id":2,"user_id":1,"name":"Appointment 2","created_at":"2018-03-31 00:00:00","updated_at":"2018-03-31 00:00:00"}]
In the view I've got:
<select name="assessment" required>
<option selected>Select...</option>
@foreach($assessments as $assessment)
@if ($assessment->name == 'Appointment 1')
<option value="Appointment 1">Appointment 1</option>
@endif
@if ($assessment->name == 'Appointment 2')
<option value="Appointment 2">Appointment 2</option>
@endif
@if ($assessment->name == 'Appointment 3')
<option value="Appointment 3">Appointment 3</option>
@endif
@endforeach
<option value="Follow Up Phone Call">Follow Up Phone Call</option>
<option value="Home Assessment">Home Assessment</option> -->
</select>
If the if statement equals to True, I don't know if you can put a continue to bypass in the foreach loop? otherwise, it displays values mulitple times from loop.
Upvotes: 0
Views: 1740
Reputation: 998
Let's assume that in the controller, you have got your results and put it in $assesments
variable. One thing you can do is that you can define a new array and send it to your view:
$assesmentNames = [];
foreach($assesments as $assesment) {
$assesmentNames[$assesment->name] = $assesment->name;
}
return view('myview', compact('assesments', 'assesmentNames'));
Now automatically the duplicates are gone. Then do this in your view:
@foreach($assessmentNames as $assessmentName)
<option value="{{ $assessmentName }}"> {{ $assessmentName }} </option>
@endforeach
Upvotes: 1
Reputation: 149
your may use "continue" statement in simple php tags within loop if you want to skip the particular iteration
Upvotes: 0