Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined method File::save() (Laravel model)

I am using Laravel to build a new web project. I am using Eloquent (its ORM) to do all the database related stuff. I have a SQLite database with two tables: 'images' and 'files'. Therefore, I have two models: 'Image.php' (class Image extends Eloquent) and 'File.php' (class File extends Eloquent).

According to the documentation I am doing it right. I tried to use the Image model and works perfect. Example of typical use of the model:

$image = new Image;
$image->val1 = $val1;
$image->val2 = $val2;
$image->save();

However and for some reason I do not know, the File model is not working as expected. I checked everything: table name, class name, file name, tables... and seems okay for me. I tried to do basically the same:

$file = new File;
$file->val1 = $val1;
$file->val2 = $val2;
$file->save();

When trying to run this, I get:

Call to undefined method Illuminate\Support\Facades\File::save()

If I do a var_dump() just before the save, it seems that the model is being loaded:

object(Illuminate\Support\Facades\File)#133 (4) {
    ["val1"]=> string(8) "abcdef" 
    ["val2"]=> string(10) "ghijkl" 
}

What am I missing here?

like image 903
Bob Dem Avatar asked Sep 09 '13 14:09

Bob Dem


1 Answers

File is a reserved word in that case because it is the alias for the below facade (defined in /app/config/app.php)

'File' => 'Illuminate\Support\Facades\File',

change the model name to something else, and all of your code should work fine.

like image 147
Gadoma Avatar answered Oct 24 '22 09:10

Gadoma