I have many to many relationship between categories and movies. When I check multiple values for categories and insert movie the result on home page selects only one category not all. I tried many things but I wasn't able to resolve this. Here is my code.
upload.blade.php:
<div class="form-group">
{!! Form::label('category_id', 'Category:') !!}
{!! Form::select('category_id', $categories, null, ['class'=>'form-control', 'multiple' => true]) !!}
</div>
Controller:
public function index(Request $request)
{
$categories = Category::pluck('category_name', 'id')->all();
return view('movies.upload', compact('categories'));
}
public function upload(MovieUploadRequest $request)
{
DB::beginTransaction();
try {
$movie = Movie::create($request->all());
$movie->categories()->attach($request->get('category_id'));
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
}
return redirect('home');
}