首页 > 解决方案 > SQLSTATE[42S22]:在 Laravel 中找不到列

问题描述

您好以下是我的查询,但运行时出现错误:

$out_let_product = OutLet::where('id', Redis::get('out_let_id') )->with(
    ['products' => function($query){
        $query->with(['prices', 'combinations' => function($query){
            $query->where('prices.active', '=', '1');                
        }]);
    }])->get();

在上面的代码中,“价格”和“组合”=产品模型中的功能。它运行良好,但问题是当我在其中包含“ $query->where('prices.active', '=', '1'); ”这一行时,系统引发如下错误:

“消息”:“SQLSTATE [42S22]:未找到列:1054 'where 子句'中的未知列'prices.active'(SQL:select * from product_combinationswhere product_combinations. product_idin (1, 2, 3) and prices. active= 1 and product_combinations. deleted_atis无效的)”,

标签: laravellaravel-5eloquent

解决方案


哦,是的,我得到了我的解决方案:

$out_let_product = OutLet::where('id', Redis::get('out_let_id') )->with(
    ['products' => function($query){
        $query->with(['prices' => function($query){
            $query->where('product_prices.active', '=', '1'); //product_prices is DB: Table name

        }]);
        $query->with(['combinations' => function($query){
            query->where('product_combinations.id', '1');  //product_combinations is DB: Table name
        }]);
    }])->get();

推荐阅读