Search code examples
phplaravel-5.3

Problems with saving in a many to many relatinship in laravel 5.3 with buttons or checkboxes


i'm having problems with saving in a pivot of a many to many relationship table. in my app i have a series of projects that have many tasks. for a certain project with its tasks, i want the auth user to be able to have assigned the wanted tasks.

so here are my models

public function tasks(){ 
        return $this->belongsToMany('App\Task', 'user_task'); 
    }
}

in User model

public function users(){ 

        return $this->belongsToMany('App\User', 'user_task'); 
    }

in Task model

    foreach($project->tasks as $task)
 <form role="form" method="POST" action="{{ route('tasks.assign', ['taskId'=>$task->id]) }}">
                                             {{-- <button class="btn btn-sm btn-success">
                                                <span class="glyphicon glyphicon-star-empty" aria-hidden="true"></span>
                                            </button> --}} 
                                            {{ csrf_field() }}

                                                <div class="checkbox">
                                                   <label>
                                                     <input type="checkbox"> {{ $task->name }}
                                                   </label>
                                                   <label> <p>{{ $task->description }}</p></label>
                                                 </div>

                                            @endforeach
                                            <input class = "btn btn-success" type="submit" value="Submit">
                                        </form>  

and here is my function in the TaskController that is supposed to save in the pivot table

public function joinTask(Request $request, Task $task)
        {

            $user = Auth::user();

            $user->tasks()->attach($task);

                //$tasks = $this->request->input('tasks', []);

                //$user->tasks()->attach($tasks);

            Session::flash('success','task asignat cu success');

            return redirect()->route('tasks.index')->withProject($project)->withTasks($tasks);
        }

I also want to mention that i am super new to laravel, so this project is somehow my first laravel project ( i use laravel 5.3)


Solution

  • First thing I'd verify in your setup is that you use the same "task" variable in your route, in your post form, and in your controller method. Right now it seems like you are trying to pass variable named $taskId into the route:

    <form role="form" method="POST" action="{{ route('tasks.assign', ['taskId'=>$task->id]) }}">
    

    , but you expect Laravel to auto-instantiate Task model based on $task variable in controller method.

    public function joinTask(Request $request, Task $task)
    

    Taking a look at your route definition would help further debug.

    A bit of an extra advice:

    One more thing I'd clean up is remove the checkbox, for two reasons: 1) looks like it does not have a "name" attribute and therefore sends nothing to server; 2) looks like your form is only submitting one task, and that is done via the route / url, so no need for extra controls, only task name and the submit button.

    So a plain GET form would suffice in this case.