OunknownO
OunknownO

Reputation: 1206

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?

Upvotes: 5

Views: 10868

Answers (3)

felix tm
felix tm

Reputation: 15

may be File::find($id)->delete();

Upvotes: 1

The Alpha
The Alpha

Reputation: 146249

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();
}

Upvotes: 5

Mina Abadir
Mina Abadir

Reputation: 2981

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

File::destroy($id)

Upvotes: 7

Related Questions