Reputation: 2028
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!');
}
Upvotes: 0
Views: 8644
Reputation: 904
::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!
Upvotes: 3
Reputation: 5042
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!!
Upvotes: 2
Reputation: 304
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();
Upvotes: 1