0
votes

I am getting an error when i click the delete button which calls Controller@delete method on a controller.

Call to undefined method Illuminate\Database\Query\Builder::destroy()

throw new BadMethodCallException("Call to undefined method {$className}::{$method}()");

destroy method

public function destroy($id)
{
     User::where('role', 'admin')->destroy($id);
     return redirect('/home')->with('message', 'Deleted!');
}
3
Try: User::find($id)->delete(); Hope this fixed it!Hiren Gohel
@HirenGohel sorry i forgot to include the where('role', 'admin') part. that part is actually causing error.Raj
Try: $user = User::where('role', 'admin')->where('id', $id); $user->delete();Hiren Gohel

3 Answers

2
votes

First find the record from DB, like:

$user = User::where('role', 'admin')->where('id', $id);

And then delete it, like:

$user->delete();

Hope this will fixed your issue!!

2
votes

::delete() can be used only on Illuminate\Database\Eloquent\Model instance. When you use where() you get Illuminate\Database\Query\Builder. That's the reason why you're getting error. Use ->delete() instead ->destroy() and it will work fine!

1
votes

You only can destroy an existing model using key. If you want filters then use delete instead.

laravel docs:

Deleting An Existing Model By Key

User::destroy(1);

User::destroy(array(1, 2, 3));

User::destroy(1, 2, 3);

Of course, you may also run a delete query on a set of models:

$affectedRows = User::where('votes', '>', 100)->delete();