All checks were successful
Deploy preview (main_go) / deploy (push) Successful in 14s
- SHJ-9: 日次売上集計処理 - SHJ-10: 年次月次売上集計処理 - SHJ-6: サーバ死活監視処理 - 各種モデルサービスコマンド追加 - earnings_summary, device, hardware_check_log, print_job_log テーブル用SQL追加
43 lines
1.0 KiB
PHP
43 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Concerns;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
/**
|
|
* ソート用トレイト
|
|
* - 画面の sort/sort_type 入力に合わせて安全に orderBy を適用
|
|
*/
|
|
trait HasSortable
|
|
{
|
|
/**
|
|
* 安全なソート適用
|
|
*
|
|
* @param Builder $query
|
|
* @param string|null $column
|
|
* @param string|null $direction
|
|
* @param array<string> $allowList 許可カラム(省略時は無制限だが推奨しない)
|
|
*/
|
|
public function scopeApplySort(Builder $query, ?string $column, ?string $direction, array $allowList = []): Builder
|
|
{
|
|
if (empty($column)) {
|
|
return $query;
|
|
}
|
|
|
|
$dir = strtolower($direction ?? 'asc');
|
|
if (!in_array($dir, ['asc', 'desc'], true)) {
|
|
$dir = 'asc';
|
|
}
|
|
|
|
if (!empty($allowList) && !in_array($column, $allowList, true)) {
|
|
return $query; // 許可されていないカラムは無視
|
|
}
|
|
|
|
return $query->orderBy($column, $dir);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|