2017-01-25 27 views
1

laravel 5.3のブレードでoptgroupをアレイで使用する方法は?laravelのブレードでoptgroupをアレイで使用する方法

私のプロジェクトではselect2を使用しています。例について

Controller

<select class="form-control select2"> 
    <optgroup label="Top Airport"> 
    <option value="MHD">Mashhad</option> 
    <option value="THR">Tehran</option> 
    </optgroup> 
    <optgroup label="All Airport"> 
    <option value="MHD">Mashhad</option> 
    <option value="THR">Tehran</option> 
    <option value="LON">London</option> 
    . 
    . 
    . 
    </optgroup> 
</select> 

index.blade.php

public function index() 
{ 
    $airport = Airport::where('status', 1)->pluck('city', 'iata'); 
    return view($this -> path_site_theme() . '.home.index', ['airport' => $airport]); 
} 

Form::select()アレイを期待しながら

{{ Form::select('from', ['Top Airport' => ['MHD', 'Mashhad', 'THR' => 'Tehran'], 'All Airport' => $airport], null, ['class' => 'form-control select2']) }} 
+0

問題が何ですか。 Form :: select()はあなたに期待される出力を与えていないのですか? –

答えて

2

pluck方法はCollectionのインスタンスを返します。 toArray()メソッドをpluckにチェーンすることで、動作させることができます。

$airport = Airport::where('status', 1)->pluck('city', 'iata')->toArray(); 

また、PHP7を使用している場合は、合体演算子を使用することをお勧めします。ヌルを配列に変換しようとすると、toArray()で多く発生するアクティブな空港がデータベースになくても、決して失敗しません。

$airport = Airport::where('status', 1)->pluck('city', 'iata')->toArray() ?? []; 
関連する問題