F
F
freeeeez2016-06-02 12:17:18
MySQL
freeeeez, 2016-06-02 12:17:18

How to tweak a DB query in Laravel?

I'm doing a catalog search. Everything works only because of the where-orWhere conditions, the logic of choosing the departure date (departure) gets lost - the entered data is ignored. There are three types of order (type1, type2, type3), the client can search in the search filter for both orders of the same type and mixed types. I need to search for all the entered types of applications and at the same time be within the specified time frame.

$types1=array(1,2); // Тип 1 (номера 1 и 2)
$types2=array(1,3,6); //Тип 2 (номера 1, 3 и 6)
$types3=array(2,5,7); //Тип 3 (номера 2,5 и 7)
$query=DB::table('search')
      ->where('departure','>',date('Y-m-d H:i:s',strtotime($date_start)))//дата выезда
      ->where('departure','<',date('Y-m-d H:i:s',strtotime($date_end)+86399))//последний день + 23 часа 59 минут
      ->where('active',1)//только активные
//далее надо выбрать из указанных типов
      ->where(function ($type1) use ($types1) {
                                        $type1->whereIn('transport',$types1));
                                        })
      ->orWhere(function ($type2) use ($types2) {
                                        $type2->whereIn('transport',$types2);
                                        })
      ->orWhere(function ($type3) use ($types3) {
                                        $type3->whereIn('transport',$types3);
                                         })
//конец выбора типов
      ->orderBy('created_at', 'desc')
      ->paginate(20);

How to implement such a request differently or fix this one?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2016-06-02
@freeeeez

It should be like this:

$types1=array(1,2); // Тип 1 (номера 1 и 2)
$types2=array(1,3,6); //Тип 2 (номера 1, 3 и 6)
$types3=array(2,5,7); //Тип 3 (номера 2,5 и 7)
$query=DB::table('search')
      ->where('departure','>',date('Y-m-d H:i:s',strtotime($date_start)))//дата выезда
      ->where('departure','<',date('Y-m-d H:i:s',strtotime($date_end)+86399))//последний день + 23 часа 59 минут
      ->where('active',1)//только активные
//далее надо выбрать из указанных типов
      ->where(function ($q) use ($types1, $types2, $types3) {
        $q->where(function ($type1) use ($types1) {
                                    $type1->whereIn('transport',$types1));
                                    })
        ->orWhere(function ($type2) use ($types2) {
                                    $type2->whereIn('transport',$types2);
                                    })
        ->orWhere(function ($type3) use ($types3) {
                                    $type3->whereIn('transport',$types3);
                                     });
      })
//конец выбора типов
      ->orderBy('created_at', 'desc')
      ->paginate(20);

That is, you need to group where

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question