摘要Laravel ORM关系法“BelongsToMany”抛出错误
我试图调用关系时收到以下错误:
类照亮\数据库对象\雄辩\关系\ BelongsToMany 不能转换成字符串
我的设置是非常基本的,包括两个型号,User
和的。
用户模型[user.php的]
<?php
use Illuminate\Auth\UserInterface;
class User extends Eloquent implements UserInterface {
protected $table = 'users';
protected $hidden = array('password');
protected $fillable = array('id', 'username', 'password');
public function getAuthIdentifier() {
return $this->getKey();
}
public function getAuthPassword() {
return $this->password;
}
}
角色模型[Role.php]
<?php
class Role extends Eloquent {
protected $table = "roles";
protected $fillable = array(
'id',
'code',
'name'
);
public function foo() {
return $this->belongsToMany('User', 'map_role_user', 'role_id', 'user_id');
}
}
最后我打电话路由文件的方法foo
,例如:
Route::get('role', function() {
return Role::find(1)->foo();
});
试试这个角色'::发现(1) - > foo' – Anam
这正是它。干杯! – Chris