Reputation: 119
I have categories table, there are columns id and category_name, how can I pass only data without column name please check screenshot below, so I want like there will be dropdown with select option, when I will select specific category_name, it will save its id to the database, example if I select category "sports" it will take id "1", please help me based on Laravel
Here is my controller
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);
}
My View
<div class="form-group">
{{Form::label('categories', 'Select Category')}}
{{Form::select('categories',$categories,null,array('multiple'=>'multiple','name'=>'categories[]'))}}
</div>
Upvotes: 0
Views: 348
Reputation: 1814
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>
Upvotes: 0
Reputation: 2212
Please try this.
On your Controller:
$categories = Category::pluck('category_name','id');
return view('pages.clubs.create_club', compact('categories'));
On your Blade:
<div class="form-group">
{{Form::label('categories', 'Select Category')}}
{{Form::select('categories[]',$categories,null,['multiple'=>'multiple']))}}
</div>
Please note that you do not need the 'name'=>'categories[]'
attribute.
Upvotes: 0
Reputation: 3274
You can use pluck to get values in the drop-down,
$categories = Category::pluck('category_name','id');
Reference : https://laravel.com/docs/5.8/collections#method-pluck
Upvotes: 1