You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
19 lines
498 B
PHP
19 lines
498 B
PHP
<?php
|
|
|
|
namespace App\Helper\Traits\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
trait CanMultiOrderBy {
|
|
public function scopeMultiOrderBy(Builder $queryBuilder, ?Array $orderBys, string $defaultOrderBy = '') {
|
|
$orderBys = $orderBys ?? [];
|
|
if(!empty($orderBys)) {
|
|
foreach($orderBys as $column => $direction) {
|
|
$queryBuilder->orderBy($column, $direction);
|
|
}
|
|
} else if($defaultOrderBy) {
|
|
$queryBuilder->orderByRaw($defaultOrderBy);
|
|
}
|
|
}
|
|
}
|