Reputation: 1782
I have two types of users: User
and SuperUser
. The SuperUser
is meant to inherit from User
and have additional properties and functions. For example, User
has a name
; SuperUser
has name
and surname
. However, when I attempt to insert something into the SuperUsers
table it does not insert it - rather an error occurs and it is not inserted.
$user= new SuperUser;
$user->name = "Foo";
$user->surname = "Bar";
$user->save();
Upvotes: 0
Views: 141
Reputation: 40909
Looking at the code, I'm guessing that the error you're getting is related to Eloquent trying to save the object in non-existent table.
When you define an Eloquent model, Eloquent assumes that this model should be stored in a table with the same name as model, but snake-cased and in plural form. Therefore, when you try to save a User object, it tries to save it in users table, but when you try to save a SuperUser object, Eloquent tries to save it in super_users table.
If you want to have class hierarchy that represents hierarchy of users, but want to have all of them saved to the same table, you need to define the table name in the common parent model class - User in your case. You can do that by setting a value to $table variable:
class User extends Model {
protected $table = 'users';
}
Upvotes: 1