简体   繁体   English

Laravel addGlobalScope与关系

[英]Laravel addGlobalScope with relationships

Good afternoon, 下午好,

I have the following tables with relationships: 我有下表与关系:

users 使用者

 class User extends Authenticatable

        public function profiles()
        {
            return $this->hasOne(Profile::class);
        }

        public function sites()
        {
            return $this->belongsToMany(Site::class);
        }

profiles 型材

class Profile extends Model

    public function users()
    {
        return $this->belongsTo(User::class);
    }

sites 网站

class Site extends Model

    public function users()
    {
        return $this->belongsToMany(User::class);
    }

In the database there is a site_user table with site_id and user_id columns. 在数据库中,有一个site_user表,其中包含site_id和user_id列。 I would like to create a global scope in each of the Profile and User models. 我想在每个Profile和User模型中创建一个全局范围。 The aim of the global scope is as follows: 全球范围的目标如下:

  • Profile Model Filter out the profiles where the auth()->user->site->id (array of results as user may belong to many sites) are equal to the profile->user->site->id (array of results). 配置文件模型筛选出以下配置文件,其中auth()-> user-> site-> id(结果数组,因为用户可能属于多个站点)等于profile-> user-> site-> id(结果数组) )。

  • User Model Filter out the users where the auth()->user->site->id (array of results as user may belong to many sites) is equal to the user->site->id (array of results). 用户模型筛选出用户,其中auth()-> user-> site-> id(结果数组,因为用户可能属于多个站点)等于user-> site-> id(结果数组)。

      public static function boot() { parent::boot(); static::addGlobalScope('scope_site_id', function (Builder $builder) { $builder->where('site_id', '=', [auth()->user()->sites->pluck('id')]); }); } 

I can get this to work if I have a column in the users and profiles tables (site_id) and reference this however this is only for 1 site and the user may belong to many. 如果我在用户和个人资料表中有一列(site_id)并引用了此列,则可以使它正常工作,但这仅适用于1个站点,并且该用户可能属于许多站点。

Thank you in advance for any advice. 预先感谢您的任何建议。

Best regards 最好的祝福

I pulled the global scope out of the Model and created a Scope file in the /app. 我从模型中拉出了全局范围,并在/ app中创建了一个范围文件。 as follows: 如下:

<?php

namespace App\Scopes;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class UserScope implements Scope
{
        public function apply(Builder $builder, Model $model)
        {

            $authUser = auth()->user()->sites()->select('id')->get()->pluck('id')->toArray();
            $builder->whereHas('sites', function($q) use($authUser) {
                $q->whereIn('id', $authUser);
            });
        }
}

The problem I was having was using where instead of whereHas and whereIn as per above. 我遇到的问题是使用where代替上述的whereHas和whereIn。

Then in the User Model include the following: 然后在用户模型中包括以下内容:

use App\Scopes\UserScope;

    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope(new UserScope);
    }

Spartieboy your answer was correct for returning the users site id as an array. Spartieboy,您的答案对于以数组形式返回用户站点ID是正确的。 Thanks for that. 感谢那。

The only problem is that when I log out of the app and log back in I get the following error: 唯一的问题是,当我注销该应用程序并重新登录时,出现以下错误:

"Call to a member function sites() on null" - hitting the following code: “在空函数上调用成员函数sites()”-击中以下代码:

            $authUser = auth()->user()->sites()->select('id')->get()->pluck('id')->toArray();

Update 更新资料

For login issue change User Model as per below: 对于登录问题,请按照以下说明更改用户模型:


use App\Scopes\UserScope;

    protected static function boot()
    {

        parent::boot();

        if (!app()->runningInConsole() && Auth::check()) {

            static::addGlobalScope(new UserScope);
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM