Last active
May 25, 2022 13:04
-
-
Save danilopinotti/b1ef467322efbabe9f1b5439639d1e2e to your computer and use it in GitHub Desktop.
Criterion to order a given query by a priority trend. This class may be applied in Laravel Query Builder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\QueryBuilderCriteria; | |
use App\Contracts\QueryBuilderCriteria; | |
use Illuminate\Database\PostgresConnection; | |
class OrderByPriorityTrendCriterion extends QueryBuilderCriteria | |
{ | |
public function apply($queryBuilder) | |
{ | |
if (!$queryBuilder->getConnection() instanceof PostgresConnection) { | |
return $queryBuilder->orderByRaw('-LOG(1.0 - RAND()) / priority'); | |
} | |
return $queryBuilder->orderByRaw('-LOG(1.0 - RANDOM()) / priority'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment