6

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?

3
  • Make sure the class File doesn't already exist somewhere else. Commented Sep 9, 2013 at 14:42
  • Did you add the extends definition for File? Commented Sep 9, 2013 at 14:42
  • I've seen this when a migration file class was named the same as a model class. Commented Dec 4, 2014 at 5:28

3 Answers 3

13

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.

Sign up to request clarification or add additional context in comments.

2 Comments

How could I miss this... thank you very much (will accept this answer).
The same goes for Event, where I had a problem: Call to undefined method Illuminate\Support\Facades\Event::save()
0

This problem occurs if you are using any reserved keywords as Model name. For eg:

Request

File ..etc

Comments

-1

For guys who are searching similar problem, remember named of your model class need be singular.

class Client extends Eloquent {

protected $table = 'promociones';        

}

then, in your controller:

client = new Client;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.