skip to Main Content

Hello Following is my query, but I get error while run it:

$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();

In above code ‘prices’ and ‘combinations’ = function in product model. and it’s working perfectly but the problem is when I include ” $query->where(‘prices.active’, ‘=’, ‘1’); ” this line inside, system raised error like below:

“message”: “SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘prices.active’ in ‘where clause’ (SQL: select * from product_combinations where product_combinations.product_id in (1, 2, 3) and prices.active = 1 and product_combinations.deleted_at is null)”,

2

Answers


  1. Chosen as BEST ANSWER

    Ohh, yes I got my solutions:

    $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();
    

  2. I’m guessing combinations and prices refer to different tables and you are using price.active in the combinations. Try using this,

    $out_let_product = OutLet::where('id', Redis::get('out_let_id') )->with(
        ['products' => function($query){
            $query->with(['combinations' => function($query){
                $query->select('combinations.*', 'prices.*')
                      ->join('prices', function($join) {
                          $join->on('prices.id' '=', 'combination.id') // or your join logic
                               ->where('prices.active', '=', 1)                    
                      })->where(// where clause for combination);
        }])->get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search