简体   繁体   English

使用按钮或复选框在laravel 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. 对于具有任务的某个项目,我希望auth用户能够分配所需的任务。

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 这是我应该在TaskController中保存在数据透视表中的函数

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) 我还想提到我是laravel的新手,所以这个项目是我的第一个laravel项目(我使用laravel 5.3)

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: 现在看来,您正在尝试将名为$taskId变量$taskId到路由中:

<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. ,但是您希望Laravel在控制器方法中基于$task变量自动实例化Task模型。

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; 我还要清理的一件事是删除该复选框,这有两个原因:1)看起来它没有“名称”属性,因此什么也不发送给服务器; 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. 2)看起来您的表单仅提交一个任务,并且通过路由/ URL完成,因此不需要额外的控件,只需任务名称和提交按钮即可。

So a plain GET form would suffice in this case. 因此,在这种情况下,简单的GET形式就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM