I have an edit account page, and I want to put the value in the database as selected :
@foreach($all_orga as $val)
@if($val->id_organisme==13)
<OPTION selected value="{{$val->id_organisme}}">{{$val->nom}}</OPTION>
@else
<OPTION value="{{$val->id_organisme}}">{{$val->nom}}</OPTION>
@endif
@endforeach
This works fine as it displays the list but nothing is "selected", so I edited it using $compte->id_organisme, which is equal 3 in my test:
@foreach($all_orga as $val)
@if($val->id_organisme == $compte->id_organisme)
<OPTION selected value="{{$val->id_organisme}}">{{$val->nom}}</OPTION>
@else
<OPTION value="{{$val->id_organisme}}">{{$val->nom}}</OPTION>
@endif
@endforeach
It still displays the list, but again, nothing "selected".
Informations :
dd($all_orga)
EQUALS
array:12 [▼
0 => {#566 ▼
+"id_organisme": 2
+"nom": "Orga 1"
}
1 => {#515 ▶}
2 => {#565 ▶}
3 => {#564 ▶}
4 => {#563 ▶}
5 => {#562 ▶}
6 => {#561 ▶}
7 => {#560 ▶}
8 => {#559 ▶}
9 => {#558 ▶}
10 => {#557 ▶}
11 => {#556 ▶}
]
dd($compte->id_organisme)
EQUALS
3
Here, the 3 value seems to be selected :
@foreach($all_orga as $val)
@if($val->id_organisme == $compte->id_organisme)
<OPTION selected value="{{$val->id_organisme}}">{{$val->nom}} {{$val->id_organisme}} {{$compte->id_organisme}}</OPTION>
@else
<OPTION value="{{$val->id_organisme}}">{{$val->nom}} {{$val->id_organisme}}</OPTION>
@endif
@endforeach
GIVES
Orga1 2
Orga2 3 3 (selected)
Orga3 4
Orga4 5
...
答案 0 :(得分:0)
我建议你让Laravel做重物。即使它不是核心的一部分,Laravel也有一个很好的表单构建器,它被转移到一个单独的项目中:Laravel Collective。并且使用它可以获得很多其他不错的功能(例如Form model binding)。
此外,您可能希望开始使用collections而不是数组。如果我在Laravel中写这个,我会按照以下方式做一些事情:
/**
* Edit account action
*
* @file YourController
*/
public function editAccountAction() {
return view('edit', [
'compte' => $compte,
'all_orga' => collect($all_orga),
]);
}
在你看来:
// use the collection's pluck method to build the array of options for the dropdown
{!! Form::select('name', $all_orga->pluck('id_organisme', 'nom'), $compte->id_organisme) !!}