Static call to Laravel delete ()

I got this error:

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

Here is the code of 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 what to call correctly?

+4
source share
2 answers

You have the following:

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

But you have to do this:

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

if($file) {

    return $file->delete();
}
+3
source

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

File::destroy($id)
+2
source

Source: https://habr.com/ru/post/1662526/


All Articles