I have 2 models.
User:
class User extends Eloquent implements UserInterface, RemindableInterface {
protected $table = 'users';
public function group ()
{
return $this->belongsTo('Group');
}
}
And Group:
class Group extends Eloquent {
protected $table = 'groups';
}
The user table has a foreign key on the group
column which references the id
on the group table.
Schema may help
Schema::table('users', function ($table) {
$table->foreign('group')
->references('id')
->on('groups')
->onDelete('cascade');
});
When I try to get the group of a user using:
User::find(1)->group()->name
I get the error
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name
Group Schema as requested:
Schema::create('groups', function ($table) {
$table->increments('id');
$table->string('name');
$table->integer('permission');
});
Here are some screens:
users table
groups table:
And the users table FK
EDIT
@Cryode solved it for me in a chat session. The problem was that I had conflicting names. The model method used the same name as the column name (group
). Simply changing the column name in the database solved the issue.