摘要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