从,project_user数据透视表中删除用户

时间:2019-02-04 15:07:37

标签: laravel eloquent

我正在尝试从项目中删除用户,我的项目具有不同的用户,我知道我应该使用detach方法,但是我真的不知道如何去做,试图了解路由和,那么表单操作中将包含哪个URL。    这是“删除”表单

            <h5>Team Members</h5>
              <ol class="list-unstyled">
                @foreach ($project->addedUsers as $user)
                <li><a href="/projects"> {{ $user->name }}</a>

                 <a href="#"
                 onclick="
                 var result =confirm('Are you sure you want to delete this User?');
                 if(result){
                     event.preventDefault();
                     document.getElementById('delete-user').submit();
                     }"
                 >
                 <i class="fas fa-minus-circle"></i></a>
                 <form id="delete-user" action="/projects/{{$project->id}}/deleteuser" method="post" display="none">
                     @csrf
                     @method('DELETE')
                     <input type="hidden" name="project_id" value="{{$project->id}}">
                     <input type="hidden" name="user_id" value="{{$user->id}}">
                     </form>
                </li>

                @endforeach

ProjectController

  public function deleteuser(Project $project){
           dd('hello');
           $project = Project::findorfail(request('project_id'));
           $user= User::where('id', request('user_id'))->first();
                $project-> addedUsers()->detach($user);

                return redirect('projects.show')->with('success', 'User deleted successful');
               }

路由文件

   Route::delete('projects/{$project}/deleteuser', 'ProjectsController@deleteuser');

我知道大多数情况都是错误的,但是也许您可以将我引向正确的方向

1 个答案:

答案 0 :(得分:0)

经过大量的反复试验,我终于使它工作了,大声笑

项目负责人

  public function deleteuser(Request $request){

           $project = Project::findorfail(request('project_id'));
           $user= User::where('id', request('user_id'))->first();
                $project-> addedUsers()->detach($user);

                return back() ->with('success', 'User deleted successful');
               }

路线

 Route::delete('projects/{id}/deleteuser', 'ProjectsController@deleteuser');

删除表格

 <h5>Team Members</h5>
                      <ol class="list-unstyled">
                        @foreach ($project->addedUsers as $user)
                        <li><a href="/projects"> {{ $user->name }}</a>

                         <a href="#"
                         onclick="
                         var result =confirm('Are you sure you want to delete this User?');
                         if(result){
                             event.preventDefault();
                             document.getElementById('delete-user').submit();
                             }"
                         >
                         <i class="fas fa-minus-circle"></i></a>
                         <form id="delete-user" action="/projects/{{$project->id}}/deleteuser" method="post" display="none">
                             @csrf
                             @method('DELETE')
                             <input type="hidden" name="project_id" value="{{$project->id}}">
                             <input type="hidden" name="user_id" value="{{$user->id}}">
                             </form>
                        </li>

                        @endforeach


                      </ol>
                    </div>