Laravel 5委托attachRolle

时间:2015-05-14 10:14:50

标签: laravel traits

我在laravel 5上犯了错误,我可以'解决它一段时间。我在Laravel 5中使用Zizaco / Entrust,但我无法正确设置用户模型的特征。我在调用用户模型时遇到错误。这是我的用户模型:

use Zizaco\Entrust\Traits\EntrustUserTrait; // I think it it's causing a problem.
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model {

use EntrustUserTrait; //or this

use Authenticatable, CanResetPassword;


protected $table = 'users';

protected $fillable = ['name', 'email', 'password'];

protected $hidden = ['password', 'remember_token'];

}

这就是导致错误的行:

$user->attachRole($owner);

据我所知,我的用户模型无法扩展特征方法。

错误:     调用未定义的方法Illuminate \ Database \ Eloquent \ Collection :: attachRole()

有什么建议吗?谢谢!

1 个答案:

答案 0 :(得分:1)

问题不在于调用attachRole()本身,而是调用你所谓的内容。 $user不是模型,而是集合。您可能只需在提取用户的查询中将get()first()切换为$user = User::where(...)->first(); $user->attachRole($owner); 。类似的东西:

{{1}}