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?

4

3 回答 3

12

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.

于 2013-09-09T14:44:08.287 回答
0

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

Request

File ..etc

于 2014-06-10T14:15:13.950 回答
-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;
于 2014-12-02T00:36:32.163 回答