WoolleyJumper
WoolleyJumper

Reputation: 3

laravel deleting user (cant get the id of the user)

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

Answers (5)

vishal pardeshi
vishal pardeshi

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

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

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

Chukwuemeka Inya
Chukwuemeka Inya

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

Kuldeep Mishra
Kuldeep Mishra

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

Priya
Priya

Reputation: 1470

Please try

public function destroy($id)
{

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

    return redirect('/staff');
}

Upvotes: 5

Related Questions