Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.1多租户设置_Php_Laravel_Laravel 5_Eloquent - Fatal编程技术网

Php Laravel 5.1多租户设置

Php Laravel 5.1多租户设置,php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,我使用的是Laravel 5.1,并使用trait和scope设置了多租户数据库,如下所示。我怎样才能添加到这一点上,以便所有的insert查询也能得到cust\u id参数 范围: <?php namespace App\Scopes; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\ScopeInter

我使用的是Laravel 5.1,并使用
trait
scope
设置了多租户数据库,如下所示。我怎样才能添加到这一点上,以便所有的insert查询也能得到
cust\u id
参数

范围:

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

要做到这一点,您需要覆盖默认的雄辩模型的行为。您可以修改参与创建或保存对象的方法之一

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

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

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

您好-感谢您的帮助,但它会导致错误:
MassAssignmentException in Model.php第417行:cust_id
。我需要在其他地方添加客户id吗?它需要在$fillable数组中刚刚添加到$fillable,但仍然是相同的错误。您使用的是Laravel的哪个verson?我正在查看5.1的代码,唯一可以引发此异常的情况是当模型完全受保护时,这意味着“count($this->filleble)==0&&$this->guarded=['*']”我使用的是Laravel 5.1