Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

php - 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

(最好的祝福)

  ask by Jason translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I pulled the global scope out of the Model and created a Scope file in the /app.

(我从模型中拉出了全局范围,并在/ app中创建了一个范围文件。)

as follows:

(如下:)

<?php

namespace AppScopes;
use IlluminateDatabaseEloquentScope;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentBuilder;

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 AppScopesUserScope;

    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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...