我有类别表,有列ID和category_name,如何仅传递没有列名的数据,请检查以下屏幕截图, 所以我想会有一个带有select选项的下拉列表,当我选择特定的category_name时,它将其ID保存到数据库中,例如,如果我选择类别“ sports”,它将获得ID“ 1”,请基于Laravel帮助我
这是我的控制人
public function create()
{
$categories = Category::all('category_name');
$data = array(
'weekdays' => [
'Monday'=>[
'2p.m',
'3p.m'
],
'Tuesday',
'Wednesday'
],
'categories' => $categories
);
return view('pages.clubs.create_club')->with($data);
}
我的观点
<div class="form-group">
{{Form::label('categories', 'Select Category')}}
{{Form::select('categories',$categories,null,array('multiple'=>'multiple','name'=>'categories[]'))}}
</div>
答案 0 :(得分:1)
您可以使用pluck在下拉列表中获取值,
$categories = Category::pluck('category_name','id');
答案 1 :(得分:0)
请尝试这个。
在您的控制器上:
$categories = Category::pluck('category_name','id');
return view('pages.clubs.create_club', compact('categories'));
在您的刀片上:
<div class="form-group">
{{Form::label('categories', 'Select Category')}}
{{Form::select('categories[]',$categories,null,['multiple'=>'multiple']))}}
</div>
请注意,您不需要'name'=>'categories[]'
属性。
答案 2 :(得分:0)
In controller use this method..if multiple values have to be sent to the
view,
$categories=DB::table('categories')->get();
$time=DB::table('times')->get();
$params = [
'categories' => $categories,
'time' => $time,
];
return view('pages.clubs.create_club')->with($params);
In view
<label>Select Category:</label>
@foreach($categories as $item)
<option value="{{ $item->id }}">{{ $item->category_name }}</option>
@endforeach</select>
<label>Week Days:</label>
@foreach($time as $item)
<option value="{{ $item->value }}">{{ $item->value }}</option>
@endforeach</select>