K
K
kostya17042020-12-27 15:44:28
Laravel
kostya1704, 2020-12-27 15:44:28

How to build a query?

5fe87fad476e8007937036.png

$body_type = implode("','",$body_type);
    
$body_type = "'".$body_type."'";
echo $body_type;

       $poisks = DB::table('items')
 ->where('brand',$brand)
 ->where('location',$location)
 ->where('fuel_type',$fuel_type)
 ->whereBetween('price', [$price_from, $price_to])
 ->whereIn('body_type', [$body_type])
 ->whereIn('model', ['100','80','q5','a1'])
 ->orderBy('id', 'asc')
 ->paginate(5);
dd($poisks);

Good afternoon.
When ->whereIn('body_type', [$body_type]) is written like this, the result on the screen is number 2.
When echo $body_type; copy from screen and paste ->whereIn('body_type', ['Sedan','Station wagon','Hatchback','Minivan','Jeep','Minibus']) like this, the query works. I sit for half a day, don't understand ???
Why does ->whereIn('body_type', [$body_type]) give an empty result ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin B., 2020-12-27
@Kostik_1993

Because first they learn, then they write, and not vice versa.
First, $body_type was an array, then you made it a string, then wrapped it in quotes for some reason, and after all the manipulations you try to feed all this shit to the whereIn method, which expects an array from you, not a string

<?php

/**
 * Вот тут было так, в этом месте у тебя массив превратился в строку
 * $body_type = implode("','",$body_type);
 */
$body_type_string = implode("','",$body_type);
    
/**
 * Далее эту же строку поместил в кавычки. Наверное для пущей надежности
 * $body_type = "'".$body_type."'";
 */
echo $body_type_string;

 $poisks = DB::table('items')
     ->where('brand',$brand)
      ->where('location',$location)
      ->where('fuel_type',$fuel_type)
      ->whereBetween('price', [$price_from, $price_to])
      ->whereIn('body_type', $body_type)
      ->whereIn('model', ['100','80','q5','a1'])
      ->orderBy('id', 'asc')
      ->paginate(5);

dd($poisks);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question