Reputation: 3
I'm new to laravel and I'm having an issue deleting a user. I cannot get the id of the user I wish to delete. Any help is appreciated.
view
<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>
controller
public function destroy(User $user)
{
$thisuser = User::find($user->id);
$thisuser->delete();
return redirect('/staff');
}
route
Route::resource('/staff', 'User\UserController');
Upvotes: 0
Views: 694
Reputation: 334
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
Route::delete('/staff/{id}', 'UserController@destroy');
function
public function destroy($id)
{
$thisuser = User::find($id);
$thisuser->delete();
return redirect('/staff');
}
Upvotes: 0
Reputation: 21681
You should try this:
public function destroy($id)
{
//Soft delete
$thisuser = User::destroy($id);
//OR
//Permanent Delete
$thisuser = User::where('id',$id)->delete();
return redirect('/staff');
}
Upvotes: 0
Reputation: 2681
In addition to Kuldeep Mishra's solution, make sure your form is properly bound to the user you intend to delete.
Upvotes: 0
Reputation: 4040
<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>
//your route
Route::get('/staff/{id}/delete', 'User\UserController@destroy')->name('staff.destroy');
//method
public function destroy($id)
{
$thisuser = User::find($id);
$thisuser->delete();
return redirect('/staff');
}
Upvotes: 2
Reputation: 1470
Please try
public function destroy($id)
{
$thisuser = User::find($id);
$thisuser->delete();
return redirect('/staff');
}
Upvotes: 5