0

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');
}

3 Answers 3

2

To enable multiple selects, you first of need to change the name of the select input from category_id to category_id[]. This enables posting of multiple variables as an array.

The code should then look like:

<div class="form-group">
    {!! Form::label('category_id', 'Category:') !!}
    {!! Form::select('category_id[]', $categories, null, ['class'=>'form-control', 'multiple' => true]) !!}
</div>

In your controller, you will then need to loop through the posted id's:

public function upload(...){
    foreach( $request->input('category_id') AS $category_id ){
        $movie = Movie::create($request->all());
        $movie->categories()->attach($category_id);
        ...
    }
}

Edit:

The method attach() also accepts an array of ids as an argument. Thereby, you don't need to loop through the input as above, but can simply use:

public function upload(...){
    $movie = Movie::create($request->all());
    $movie->categories()->attach($request->input('category_id'));
    ...
}
Sign up to request clarification or add additional context in comments.

4 Comments

It works, thank you very much! But now I have a small problem on home page. When I insert movie with eg. two categories then second category moves to the right and enters where it should be some other <td>, it doesn't print two categories next to each other, if you know what i mean. How to solve that?
@Gacho to answer that I would need to see the markup for it. However if it works, you should accept the answer, and create a new one for your "styling issue"
@foreach($movies as $movie) <tr> <td><a href="{{ route('mov.edit', $movie->id) }}">{{$movie->name}}</a></td> @foreach($movie->categories as $category) <td><a href="{{ route('cat.edit', $category->id) }}">{{$category>category_name}}</a></td> @endforeach
Like I said. You should post this as its own question, since it has nothing to do with your original one. Comment fields are not ment to start new question threads. And ofcourse each category becomes a td, since you have <td></td> inside the foreach loop. <td>Name of movie</td><td>@foreach($category AS $cat) {{ $cat->name }} @endforeach</td>...
2

In your view, use a name like :

<select name="yourfiled[]"></select>.

In controller, to store it in database, just write

$movies->categories = json_encode(yourfield);

Done.

Comments

0

You can write this. hopefully this will solve your problem

<div class="form-group">
    {!! Form::label('category_id', 'Category:') !!}
    {!! Form::select('category_id[]', $categories, null, ['class'=>'form-control', 'multiple' => true]) !!}
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.