【问题标题】:Laravel 5.3 Eloquent where clause on joining table from parent modelLaravel 5.3 Eloquent where 从父模型连接表的子句
【发布时间】:2017-04-17 10:34:03
【问题描述】:

我有一张与一个品牌和一个季节相关联的面料表。运行App\fabrics::with('brand', 'season')->get() 可以让我获得所有面料及其品牌和季节信息,这很好(见下文)。

我想根据季节 ID 获取面料。我试过了:

App\fabrics::with('brand', 'season')->where('season.id', '1')->get()

但这不起作用。

Column not found: 1054 Unknown column 'season.id' in 'where clause' (SQL: select * from `fabrics` where `season`.`id` = 1)'

是否可以通过其中一种关系获取面料和过滤器?

显然我可以从季节模型 App::season 中获取面料,但我想知道是否可以从其父(面料)模型中获得。

所有来自 App\fabrics::with('brand', 'season')->get() 的信息

>>> App\fabrics::with('brand', 'season')->get()
=> Illuminate\Database\Eloquent\Collection {#794
     all: [
       App\Fabrics {#786
         id: 1,
         fabric_code: "GMAN01",
         fabric_design: "ANODA",
         fabric_price_group: "I",
         fabric_retail_price: "27.00",
         fabric_trade_price: null,
         fabric_width: 141,
         fabric_pattern_repeat: 1,
         fabric_fiber: "48% POLYESTER 44% COTTON 8% VISCOSE",
         fabric_online: 1,
         fabric_available: 1,
         fabric_meters_remaining: 256.78,
         fabric_virutal_meters_remaining: 216.28,
         created_at: "2016-12-02 10:52:09",
         updated_at: "2016-12-02 13:08:07",
         brand_id: 1,
         season_id: 1,
         brand: App\Brands {#742
           id: 1,
           brand_name: "*****",
           created_at: "2016-12-02 11:36:10",
           updated_at: "2016-12-02 11:36:10",
         },
         season: App\Seasons {#795
           id: 1,
           season_name: "Autumn/Winter 2016",
           season_code: "AW16",
           created_at: "2016-12-02 13:07:50",
           updated_at: "2016-12-02 13:07:50",
         },
       },
       App\Fabrics {#765
         id: 2,
         fabric_code: "GMAN02",
         fabric_design: "ANODA",
         fabric_price_group: "I",
         fabric_retail_price: "27.00",
         fabric_trade_price: null,
         fabric_width: 141,
         fabric_pattern_repeat: 1,
         fabric_fiber: "48% POLYESTER 44% COTTON 8% VISCOSE",
         fabric_online: 1,
         fabric_available: 0,
         fabric_meters_remaining: 20.0,
         fabric_virutal_meters_remaining: 17.0,
         created_at: "2016-12-02 10:52:09",
         updated_at: "2016-12-02 13:14:56",
         brand_id: 2,
         season_id: 1,
         brand: App\Brands {#770
           id: 2,
           brand_name: "*****",
           created_at: "2016-12-02 11:36:10",
           updated_at: "2016-12-02 11:36:10",
         },
         season: App\Seasons {#795},
       },
       App\Fabrics {#791
         id: 3,
         fabric_code: "JBBU01",
         fabric_design: "BURDOCK",
         fabric_price_group: "D",
         fabric_retail_price: "16.00",
         fabric_trade_price: null,
         fabric_width: 140,
         fabric_pattern_repeat: 64,
         fabric_fiber: "100% COTTON",
         fabric_online: 0,
         fabric_available: 1,
         fabric_meters_remaining: 856.1,
         fabric_virutal_meters_remaining: 856.1,
         created_at: "2016-12-02 10:52:09",
         updated_at: "2016-12-02 13:08:13",
         brand_id: 3,
         season_id: 4,
         brand: App\Brands {#787
           id: 3,
           brand_name: "*****",
           created_at: "2016-12-02 11:36:10",
           updated_at: "2016-12-02 11:36:10",
         },
         season: App\Seasons {#775
           id: 4,
           season_name: "Spring/Summer 2015",
           season_code: "SS15",
           created_at: "2016-12-02 13:07:50",
           updated_at: "2016-12-02 13:07:50",
         },
       },
     ],
   }

【问题讨论】:

    标签: php sql laravel eloquent laravel-5.3


    【解决方案1】:

    使用eager loads contraining 的闭包:

    App\fabrics::with(['brand', 'season' => function($q) use($seasonId) {
        $q->where('id', $seasonId);
    }])->get();
    

    如果您想按季节 ID 过滤面料,请使用whereHas()

    App\fabrics::with('brand', 'season')
        ->whereHas('season', function($q) use($seasonId) {
            $q->where('id', $seasonId);
        })->get();
    

    【讨论】:

    • +1 用于文档链接,我不知道该功能被称为什么,所以这会有所帮助。是否可以在 with() 上传递多个闭包?例如按季节 ID 过滤和按品牌 ID 过滤?
    • 当然,每个关系都可以使用闭包。
    • 非常感谢。我猜 $seasonId 变量是由 Eloquent 创建的?
    • @twigg 不,这是你的变量。如果需要,您可以使用整数代替它(例如您的问题中的示例)。在这种情况下,您不需要在闭包中添加 use() 子句。
    • 啊,是的,和你一起。好的,谢谢,时间允许我会接受的
    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 2022-10-08
    相关资源
    最近更新 更多