首页 > 解决方案 > 防止数据库播种的全局范围

问题描述

我正在使用 Laravel spark,并且我通过使用在 trait 中实现的范围来限制团队对模型的访问

namespace App\Scopes;

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

class TeamScope 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)
    {
        $builder->where('team_id', '=',Auth()->user()->currentTeam->id );
    }
}

我的问题是,当我运行数据库播种机时,它失败了,因为没有用户可以验证。

播种机没有任何方法可以让我登录特定用户。

有没有办法在播种时禁用全局范围,或任何其他解决方案?

谢谢

标签: laraveleloquentlaravel-spark

解决方案


如果你在种子中调用模型,你可以->withoutGlobalScopes()先调用

更多关于这里https://laravel.com/docs/5.7/eloquent#query-scopes在“删除全局范围”部分。


推荐阅读