【发布时间】:2014-01-12 05:48:07
【问题描述】:
总结
我在尝试调用关系时收到以下错误:
类 Illuminate\Database\Eloquent\Relations\BelongsToMany 的对象 无法转换为字符串
我的设置非常基础,包含两个模型,User 和 Role。
用户模型 [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();
});
【问题讨论】:
-
试试这个
Role::find(1)->foo -
就是这样。干杯!