Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel delete() static call

I got this error:

Non-static method Illuminate\Database\Eloquent\Model::delete() should not be called statically, assuming $this from incompatible context

Here is the code in my controller:

$file_db = new File();
$file_db = $file_db->where('id',$id)->find($id);
$file_db = $file_db->delete();

Can someone explain what I am doing wrong and how to call it correctly?

like image 591
OunknownO Avatar asked Dec 01 '16 11:12

OunknownO


2 Answers

If you want to delete model with specific id, use the destroy() method.

File::destroy($id)
like image 130
Mina Abadir Avatar answered Oct 21 '22 09:10

Mina Abadir


You have this:

$file_db = $file_db->where('id',$id)->find($id);

But you should be doing this:

$file = File::where('id', $id)->first(); // File::find($id)

if($file) {

    return $file->delete();
}
like image 28
The Alpha Avatar answered Oct 21 '22 09:10

The Alpha