krgm.so-manager-dev.com/app/Http/Controllers/Admin/SealsController.php
你的名字 277631c23a
All checks were successful
Deploy main / deploy (push) Successful in 24s
【シール発行履歴】ソート機能追加
2025-09-05 20:13:05 +09:00

75 lines
2.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class SealsController extends Controller
{
public function list(Request $request)
{
// ソート用パラメータ取得指定がなければnull
$sort = $request->input('sort');
$sort_type = $request->input('sort_type', 'asc');
// sealテーブルを参照し、フィルター
$query = DB::table('seal');
// フィルター:発行日
$periodType = $request->input('period_type');
$recentPeriod = $request->input('recent_period');
$sealDayFrom = $request->input('seal_day_from');
$sealDayTo = $request->input('seal_day_to');
if ($periodType === 'range' && $sealDayFrom && $sealDayTo) {
$query->whereBetween('seal_day', [$sealDayFrom, $sealDayTo]);
} elseif ($periodType === 'recent' && $recentPeriod) {
$now = date('Y-m-d');
switch ($recentPeriod) {
case '12m':
$from = date('Y-m-d', strtotime('-12 months', strtotime($now)));
break;
case '6m':
$from = date('Y-m-d', strtotime('-6 months', strtotime($now)));
break;
case '3m':
$from = date('Y-m-d', strtotime('-3 months', strtotime($now)));
break;
case '2m':
$from = date('Y-m-d', strtotime('-2 months', strtotime($now)));
break;
case '1m':
$from = date('Y-m-d', strtotime('-1 months', strtotime($now)));
break;
case '1w':
$from = date('Y-m-d', strtotime('-1 week', strtotime($now)));
break;
default:
$from = null;
}
if (isset($from)) {
$query->where('seal_day', '>=', $from);
}
} else {
// デフォルト直近3ヶ月
$now = date('Y-m-d');
$from = date('Y-m-d', strtotime('-3 months', strtotime($now)));
$query->where('seal_day', '>=', $from);
}
// ソート指定があればorderBy、なければDB物理順
if ($sort) {
$list = $query->orderBy($sort, $sort_type)->paginate(20);
} else {
$list = $query->paginate(20);
}
return view('admin.seals.list', [
'list' => $list,
'sort' => $sort,
'sort_type' => $sort_type,
]);
}
}