Raj
Raj

Reputation: 2028

Call to undefined method Illuminate\Database\Query\Builder::method() error in laravel 5

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

Answers (3)

Wolen
Wolen

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

Hiren Gohel
Hiren Gohel

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

Farrukh Ayyaz
Farrukh Ayyaz

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

Related Questions