((Concept Confusion)App \ User :: role必须返回关系实例(有效的关系工作但查询生成器不工作)]] << [

问题描述 投票:0回答:1

我正在创建AdminAuthenticated中间件

public function handle($request, Closure $next) { if (Auth::user()->role->name == 'customer') { return redirect('/home')->with('message', 'You are not allowed to access'); } return $next($request); }

我的问题是

当我使用雄辩的关系时,它将正常工作,但是当我使用查询生成器时,其抛出错误

此代码有效

public function role(){ //this code work return $this->belongsTo('App\Role'); }

此代码引发错误,为什么会发生,以及如何在下面的代码中使用(查询生成器)

public function role(){ //this code not work throw exception return DB::table('users') ->join('roles', 'roles.id', '=', 'users.role_id') ->select('roles.*') ->where('users.id','=',$this->id); }
我正在创建AdminAuthenticated中间件公共功能句柄($ request,Closure $ next){如果(Auth :: user()-> role-> name =='customer'){return redirect('/ home')-& ...
php laravel eloquent
1个回答
0
投票
因为这种关系是多对多的,所以用户和角色表之间有一个数据透视表。
© www.soinside.com 2019 - 2024. All rights reserved.