【问题标题】:Laravel global scope with relationsLaravel 全局范围与关系
【发布时间】:2015-12-02 19:14:07
【问题描述】:

我想要一个具有全局查询范围的模型,该范围的范围是多态的一对一关系。

问题是一切正常,我可以创建全局查询范围,但我没有在全局查询范围类的 apply 函数中获得关系。我想使用关系进行查询,而不仅仅是进行连接查询或其他。

有人知道这是否可行吗?

我有以下代码:

class Content extends Model {
    use Environmentabletrait;
}

trait EnvironmentableTrait {
    public static function bootEnvironmentableTrait() {
        static::addGlobalScope(new EnvironmentScope);
    }

    public function environment() {
        return $this->morphOne(Environment::class, 'environmentable');
    }
}

class Environment extends Model {
    public function environmentable() {
        return $this->morphTo();
    }
}

class EnvironmentScope implements ScopeInterface {
    public function apply(Builder $builder, Model $model) {
        $builder-> ...
    }
}

还有

我希望我已经解释得足够好了;)

在此先感谢

【问题讨论】:

  • 您好,请问我们可以在Model中的两个表之间进行内连接吗??

标签: php laravel scope relationship


【解决方案1】:

没关系,我已经用以下代码修复了它:

public function apply(Builder $builder, Model $model)
{
    return $builder->whereHas('environment', function ($query) {
        $query->where('environment', app()->environment());
    });
}

【讨论】:

    猜你喜欢
    • 2019-03-29
    • 1970-01-01
    • 2014-02-22
    • 2016-04-14
    • 1970-01-01
    • 2022-01-16
    • 2012-05-03
    • 2015-07-06
    相关资源
    最近更新 更多