laravel删除用户(无法获取用户的ID)

时间:2018-04-09 10:13:01

标签: php laravel

我是laravel的新手,我在删除用户时遇到问题。 我无法获取我想删除的用户的ID。 任何帮助表示赞赏。

视图

 <form method="post" action="/staff/{{$user->id}}">
 <input type="hidden" name="_method" value="DELETE">
 {{csrf_field()}}
 <button style="padding: 0" type="submit" class="btn btn-link margin-left-40"
 onclick="return confirm('Are you sure you want to delete {{ucfirst($user->name)}}?');">
 <i class="icmn-bin"></i> Delete</button>
 </form>

控制器

   public function destroy(User $user)
{

    $thisuser = User::find($user->id);
    $thisuser->delete();

    return redirect('/staff');
}

路线

Route::resource('/staff', 'User\UserController');

5 个答案:

答案 0 :(得分:5)

请尝试

public function destroy($id)
{

    $thisuser = User::find($id);
    $thisuser->delete();

    return redirect('/staff');
}

答案 1 :(得分:2)

<form method="post" action="{{route('staff.destroy',$user->id)}}">
<input type="hidden" name="_method" value="DELETE">
{{csrf_field()}}
<button style="padding: 0" type="submit" class="btn btn-link margin-left-40"
onclick="return confirm('Are you sure you want to delete 
 {{ucfirst($user->name)}}?');">
 <i class="icmn-bin"></i> Delete</button>
</form>

//你的路线

Route::get('/staff/{id}/delete', 'User\UserController@destroy')->name('staff.destroy');

//方法

   public function destroy($id)
 {

   $thisuser = User::find($id);
   $thisuser->delete();

   return redirect('/staff');
 }

答案 2 :(得分:0)

你应该试试这个:

public function destroy($id)
{
    //Soft delete
    $thisuser = User::destroy($id);

    //OR

    //Permanent Delete 
    $thisuser = User::where('id',$id)->delete();


    return redirect('/staff');
}

答案 3 :(得分:0)

除了Kuldeep Mishra的解决方案之外,请确保您的表单已正确绑定到您要删除的用户。

答案 4 :(得分:0)

html

 <form method="post" action="/staff/{{$user->id}}">
 <input type="hidden" name="_method" value="DELETE">
 {{csrf_field()}} 
 <button style="padding: 0" type="submit" class="btn btn-link margin-left- 
 40" onclick="return confirm('Are you sure you want to delete   
 {{ucfirst($user->name)}}?');">
 <i class="icmn-bin"></i> Delete</button>
 </form>

<强>路线

Route::delete('/staff/{id}', 'UserController@destroy');

<强>功能

public function destroy($id)
{

     $thisuser = User::find($id);
     $thisuser->delete();

     return redirect('/staff');
}