0
votes

Getting an error when im trying delete users in my table.

My Route(api.php)

     Route::delete('/users/{id}', 'UsersController@destroy');

My Controller

    public function destroy($id)
    {
        $user = UserModel::findOrFail($id);
        $user->delete();
        return new UsersCollection($user);
    }

My Vue.js

        remove(id) {
            let $this = this;
            axios.delete(`/api/users/${id}`).then(function(response) {
                $this.fetchUser();
            })
        },

Any Help will be appreciate... Thanks in advances.

4
Could you elaborate on what your error is plsIssam Rafihi
Try to place your findOrFail in a try catch blockIssam Rafihi
@IssamRafihi the point of the findOrFail is to return a 404 when the entity is not found, it is working correctly.atymic

4 Answers

1
votes

You are getting a 404 error because you are trying to load a model with the id 0, which does not exist (keys start at 1).

I would suggest checking your javascript code to ensure that the correct user ID to delete is passed, and that it's not accidently casting an empty value to 0.

1
votes
$user = UserModel::findOrFail($id); //retrieving use, dont use findorfail here because it will thrown 404 error page
$user->delete(); // you delete the user
return new UsersCollection($user); // it means $user now is an empty collection therefore you get an error

i believe after you delete user , you should return only code determine success or not like

return response()->json([
'code' => 200,
'message' => "you've successfully delete"
);

or if you want to passed the data detail of deleted user you can do like this

public function destroy($id)
{
    $user = UserModel::find($id);
    if(!is_null($user)){
        return response()->json([
             'code' => 404,
             'message' => 'Not found',
        ]);
    };
    $old_user = $user;
    $user->delete();
    return new UsersCollection($old_user);
}
1
votes
Probably you have been getting this error because you are trying to delete model that doesn't exists. You can try something like

$user = UserModel::find($id);

if ($user) {
    $user->delete();
    return new UsersCollection($user); 
}else{
   // Code if $user not found
}
0
votes

Now i solved it By change my table from

<pre>
         <tr v-for="(user, key) in users">
              <td>{{user.id}}</td>
              <td>{{user.name}}</td>
              <td>{{user.email}}</td>
              <td>{{user.phone_number}}</td>
              <td @click.prevent="update(key)">
                 <span class="btn btn-primary"><i class="mdi mdi-mode-edit" ></i></span>
              </td>
              <td @click.prevent="remove(key)">
                   <span class="btn btn-danger"><i class="mdi mdi-delete"></i></span>
               </td>
         </tr>
</pre>

to


<tr v-for="(user, key) in users">
     <td>{{user.id}}</td>
     <td>{{user.name}}</td>
     <td>{{user.email}}</td>
     <td>{{user.phone_number}}</td>
     <td @click.prevent="update(key)">
        <span class="btn btn-primary"><i class="mdi mdi-mode-edit" ></i></span>
     </td>
     <td @click.prevent="remove(user.id)">
         <span class="btn btn-danger"><i class="mdi mdi-delete"></i></span>
     </td>
 </tr>