Laravel 5.1多功能设置 [英] Laravel 5.1 Multitenancy setup

查看:142
本文介绍了Laravel 5.1多功能设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 5.1,并使用 trait scope 进行多租户数据库设置,如下所示。如何添加到这一点,以便所有插入查询也得到 cust_id 参数注入?

I am using Laravel 5.1 and have a multi-tenancy database setup using a trait and scope as below. How can I add to this so that all insert queries also get the cust_id parameter injected?

范围:

<?php

namespace App\Scopes;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\ScopeInterface;
use App\Customers;
use DB, Session;

class MultiTenantScope implements ScopeInterface
{
    /**
     * Create a new filter instance.
     *
     * @param  UsersRoles $roles
     * @return void
     */
    public function __construct()
    {
        $this->custId = Session::get('cust_id');
    }

    /**
     * Apply scope on the query.
     *
     * @param Builder $builder
     * @param Model $model
     * @return void
     */
    public function apply(Builder $builder, Model $model)
    {
        if($this->custId) 
        {
            $builder->where($model->getTable() . '.cust_id', $this->custId); 
        } 
        else 
        {
            $model = $builder->getModel();
            $builder->whereNull($model->getKeyName()); 
        }
    }

    /**
     * Remove scope from the query.
     *
     * @param Builder $builder
     * @param Model $model
     * @return void
     */
    public function remove(Builder $builder, Model $model)
    {
        $query = $builder->getQuery();
        $query->wheres = collect($query->wheres)->reject(function ($where) 
        {
            return ($where['column'] == 'cust_id');
        })->values()->all();
    }  
}

特征: p>

Trait:

<?php 

namespace App\Scopes;

trait MultiTenantTrait
{
    /**
     * Boot the scope.
     * 
     * @return void
     */
    public static function bootMultiTenantTrait()
    {
        static::addGlobalScope(new MultiTenantScope());
    }

    /**
     * Get all tenants.
     * 
     * @return string
     */
    public static function allTenants()
    {
        return (new static())->newQueryWithoutScope(new MultiTenantScope());
    }
}


推荐答案

为了做到这一点,你需要重写默认的演绎模型的行为。您可以修改参与创建对象或保存它们的方法之一。

In order to do that, you'll need to override the default Eloquent Model's behaviour. You can modify one of the methods that participate in creating object or saving it.

我的建议是覆盖默认创建行为逻辑,因为这将为您提供一个对象, strong> custId 即使在将其保存到数据库之前也会设置。

My suggestion is to override the default creation behaviour logic as this will give you an object with custId set even before you save it to the database.

实现这一点的最简单的方法是覆盖trait中的继承构造函数。为此,请将此方法添加到您的特征中:

The easiest way to achieve that is to override the inherited constructor in a trait. In order to do that, add this method to your trait:

public function __construct(array $attributes = [])
{
  parent::__construct(array_merge($attributes, ['cust_id' => $this->custId]));
}

这篇关于Laravel 5.1多功能设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆