[Vuejs]-No query results for model [App\UserModel] 0 [ Laravel ]

0πŸ‘

βœ…

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>

πŸ‘€latenight

1πŸ‘

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.

πŸ‘€atymic

1πŸ‘

$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πŸ‘

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
}

Leave a comment