mirror of
https://gitee.com/ctexthuang/hyperf-micro-svc.git
synced 2026-02-08 18:30:16 +08:00
first commit
This commit is contained in:
38
app/Repository/Traits/RepositoryOrderByTrait.php
Normal file
38
app/Repository/Traits/RepositoryOrderByTrait.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repository\Traits;
|
||||
|
||||
use Hyperf\Database\Model\Builder;
|
||||
|
||||
trait RepositoryOrderByTrait
|
||||
{
|
||||
public function handleOrderBy(Builder $query, $params): Builder
|
||||
{
|
||||
if ($this->enablePageOrderBy()) {
|
||||
$orderByField = $params[$this->getOrderByParamName()] ?? $query->getModel()->getKeyName();
|
||||
$orderByDirection = $params[$this->getOrderByDirectionParamName()] ?? 'desc';
|
||||
$query->orderBy($orderByField, $orderByDirection);
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
protected function bootRepositoryOrderByTrait(Builder $query, array $params): void
|
||||
{
|
||||
$this->handleOrderBy($query, $params);
|
||||
}
|
||||
|
||||
protected function getOrderByParamName(): string
|
||||
{
|
||||
return 'order_by';
|
||||
}
|
||||
|
||||
protected function getOrderByDirectionParamName(): string
|
||||
{
|
||||
return 'order_by_direction';
|
||||
}
|
||||
|
||||
protected function enablePageOrderBy(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user