Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahr's avatar
Level 10

Select option default

I have three tables, grades and users and educationals.

I have a grade_id field in educationals table.

User.php

public function educationals()
{
    return $this->hasMany(Educational::class);
}

my blade

@foreach(auth()->user()->educationals as $educational)
<div class="row">
<div class="col-md-2">
<div class="form-group">
<label for="grade_id">Grade</label>
<select id="grade_id" name="grade_id[]" class="form-control">
@foreach($grades as $grade)
<option value="{{ $grade->id }}" {{ $educational == $grade ? 'selected' : '' }}>
@endforeach
</select>
</div>
</div>
</div>
@endforeach

This displayed always id number of 1.

0 likes
1 reply
MichalOravec's avatar
Level 75
<option value="{{ $grade->id }}" {{ $educational->grade_id == $grade->id ? 'selected' : '' }}>

Instead of auth()->user()->educationals pass variable to the view and use eager loading.

1 like

Please or to participate in this conversation.