【问题标题】:how to get only active relationships in laravel eloquent in when如何在laravel eloquent中只获得活跃的关系
【发布时间】:2019-09-25 09:41:26
【问题描述】:

嗨,我有一个 when eloquent 子句,我想只选择具有折扣活动关系的属性,所以我的代码如下所示:

->when($has_discount, function ($query, $has_discount) {
            $query->with([
                'accommodationRoomsLimited' => function ($q) use ($has_discount) {
                    $q->has('discount');
                }
            ]);

我会去我的酒店,然后在里面我选择房间,我想选择与房间折扣有积极关系的酒店,但是这个代码现在对我没有任何作用,我怎么能做到这一点??

【问题讨论】:

  • 没有模型名称或表名称如何导出解决方案! @Farshad

标签: php laravel eloquent eloquent-relationship


【解决方案1】:

设置条件的closer函数 像这样

$query->with([
            'accommodationRoomsLimited' => function ($q) use ($has_discount) {
                $q->where('discount', 'active');
            }
        ]);

【讨论】:

    猜你喜欢
    • 2020-06-19
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    相关资源
    最近更新 更多