简体   繁体   中英

Laravel Role HasMany relationship

I have a User Model and a Role Model which are in many-to-many relation. I have two roles: admin and manager. I also have an Order Model . Managers need to have many orders. Where do I state such relations? Do I have to state it in the User Class? Do I need to create separate models for Admins and Managers?

Your many-to-many relationship between User and Role can be perfectly described with belongsToMany Eloquent relation methods both ways. Also since each Order must have responsible manager for it we also have one-to-many relation between Manager and Order which will be described with hasMany / belongsTo methods.

So, your User model will have:

public function roles()
{
    return $this->belongsToMany('App\Role');
}

public function orders()
{
    return $this->hasMany('App\Order');
}

For your Role model:

public function users()
{
    return $this->belongsToMany('App\User');
}

And lastly your Order model:

public function manager()
{
    return $this->belongsTo('App\User');
}

There is no need to create a certain limitations (like "only users with role manager can have orders") on DB schema level, it's easier to implement in the code. So, for example, you might want to implement a method that will assign order to user and check his roles first.

Managers & admins are a subset of your users, defined by their roles.

Therefore, we'll use scope to filter users who are managers by their roles.

App\\Scopes\\ManagerUserScope.php

<?php

namespace App\Scopes;

use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class ManagerUserScope implements Scope
{
    /**
     * Apply the scope to a given Eloquent query builder.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @return void
     */
    public function apply(Builder $builder, Model $model)
    {
        //Assuming Your user has a relationship (has many) role
        $builder->whereHas('role', function($roleQuery) {
            //Assuming that in the role table, the manager role entry has ID 1
            $roleQuery->where('id','=',1);
        });
    }
}

Then, we extend the User model to create a manager model which has the above scope automatically applied.

App\\Models\\Manager.php

namespace App\Models;

use App\Scopes\ManagerUserScope;

class Manager extends User {

    /**
     * The "booting" method of the model.
     *
     * @return void
     */
    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope(new ManagerUserScope);
    }

    /**
    *  Relationship: Orders (Has many)
    */
    public function orders()
    {
        return $this->hasMany('orders');
    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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