Level 75
Where did you see the code which is written like that?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I get the correct result as in CodeA, but since I do different filters with $ query, I have to use it with $ query in a structure like CodeB. How can I do that?
CodeA
\DB::select("select tableA.*, tableB.price, tableB.diff from tableA right join(SELECT id, end_date, pro_id, price, DATEDIFF(tableB.end_date, '2021-03-08') diff FROM tableB GROUP BY id order by diff asc) tableB on tableA.id = tableB.pro_id where (date(end_date) >= '2021-03-08') group by tableA.id order by price desc");
CodeB
foreach ($sort as $_sort){
if(in_array($_sort, array_keys(self::$allowedSortingOptions))){
$parts = explode(' ', $_sort);
if(count($parts) < 2){
array_push($parts, 'desc');
}
list($sortField, $sortDirection) = $parts;
if($sortField == 'price'){
\DB::select("select tableA.*, tableB.price, tableB.diff from tableA right join(SELECT id, end_date, pro_id, price, DATEDIFF(tableB.end_date, '2021-03-08') diff FROM tableB GROUP BY id order by diff asc) tableB on tableA.id = tableB.pro_id where (date(end_date) >= '2021-03-08') group by tableA.id order by price desc");
}else{
$query->orderBy($sortField, $sortDirection);
}
}
}
$lastPage = $query->paginate($perPage, $page)->lastPage();
if($lastPage < $page){
$page = 1;
}
return $query->paginate($perPage, $page);
Please or to participate in this conversation.