簡體   English   中英

laravel blade如何設置下拉列表的選項

[英]laravel blade how to set the options for drop down list

我正在嘗試使用blade創建下拉列表

我已經檢查了這個問題Laravel 4 blade下拉列表類屬性

我試過這個:

 {{Form::select('category_id', $categories)}}

結果是:

<select name="category_id"><option value="0">{"id":2,"name":"Apartment"}</option><option value="1">{"id":3,"name":"Car"}</option></select>

我不知道如何只顯示選項的name值。 加上我不知道如何將每個選項的值設置為其id

我知道第四個參數是選項一,我試着這樣做

 {{Form::select('category_id', $categories, '', $categories)}}

但我得到了這個例外:

htmlentities() expects parameter 1 to be string, array given (View: 

請注意$categories是數組,每行都有id name

更新1

我將控制器的值發送到這樣的視圖

 $categories = Category::All(['id', 'name']);

其中Category是模型

Form::select()需要像平面數組一樣

array(
  1 => 'Appartment',
  2 => 'Car'
)

$categories = Category::all()為您提供了一個看起來像的多維數組

array(
  0 => array(
    'id' => 1,
    'name' => 'Appartment'
  ),
  1 => array(
    'id' => 2,
    'name' => 'Car'
  )
)

據說只是改變

$categories = Category::all(['id', 'name']);

$categories = Category::lists('name', 'id');

然后這將工作得很好

{{ Form::select('category_id', $categories) }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM