2015-04-12 93 views
0

我是新來的PHP框架,我正在使用Laravel創建一個CRUD應用程序。問題是,當我試圖通過從DB從View值一類的Id我得到的錯誤:Laravel 5將ID從數據庫傳遞到<select>的值

htmlentities() expects parameter 1 to be string, array given (View: C:\wamp\www\vecrud\resources\views\products\create.blade.php)

控制器

public function create() 
{ 
    $products_create = categories::all()->lists('category'); 
    $categ_id = categories::all()->lists('id'); 

    return View::make('products.create', compact('products_create', 'categ_id')); 
} 

查看

{!! Form::label('category', 'Categorie') !!} <br /> 
{!! Form::select('category', $products_create, Input::old('category'), array('value' => $categ_id)) !!} 

回答

2

值(id)和選擇選項(category)的文本應該在同一個數組中。我想你正在尋找這樣的:

$category_list = categories::all()->lists('category', 'id'); 

return View::make('products.create', compact('category_list')); 

查看:

{!! Form::select('category', $category_list, Input::old('category')) !!} 

而且你不需要調用all()lists()

$category_list = Categories::lists('category', 'id'); 
1

使用lists()方法,第一個參數是數組值,可選的第二個參數是用於數組鍵的屬性。所以,如果你想從主鍵鍵入一個數據庫值,那麼你可以做:

$options = $model->lists('name', 'id'); 

然後,您可以只是直接通過這個陣列到select形式助手:

Form::select('name', $options, null, ['class' => 'form-control']); 

希望幫助!

相關問題