$itemListBase = $itemListBase->with($queryMapModelText)->get()
->sort(function($a, $b) use ($queryText, $orderDir, $orderCol) {
$queryTextTitle = ‘item_title’;
$queryTableName = explode(“.”, $queryText)[0];
$queryFieldName = explode(“.”, $queryText)[1];
if ($orderCol == “rating”) { // Ratings are numeric; Don’t compare as strings
$testCase = -1;
} else {
$testCase = “”;
}
$queryItem1 = is_null($a->$queryTableName) ? $testCase : (is_null($a->$queryTableName->$queryFieldName) ? $testCase : $a->$queryTableName->$queryFieldName);
$queryItem2 = is_null($b->$queryTableName) ? $testCase : (is_null($b->$queryTableName->$queryFieldName) ? $testCase : $b->$queryTableName->$queryFieldName);
if ($orderCol == “rating”) {
$sortResultVal = ($queryItem1 < $queryItem2) ? -1 : (($queryItem1 > $queryItem2) ? 1 : 0);
} else {
$sortResultVal = strcmp($queryItem1, $queryItem2);
}
if ($sortResultVal === 0) {
$sortResultVal = strtolower($a->$queryTextTitle . “z”) > strtolower($b->$queryTextTitle . “z”) ? 1 : -1;
} else {
$sortResultVal = ($orderDir == ‘asc’) ? $sortResultVal : -1 * $sortResultVal;
}
return $sortResultVal;
});
Keith D Commiskey
https://keithdc.com