問題
・日付検索をしたい
・1月1日~1月5日の間、1月1日以降など、間や以降などの処理を行いたい
ゴール
参考サイト
手順
①ルーティング
web.php
Route::resource('/search', 'SearchController');
②コントローラー
SearchController.php
public function index(Request $request)
{
// ---------- 支出一覧表示 ----------
// 空を用意することで「 共通処理①」で値がなくてもエラーにならない
$spendings = [];
// 支出テーブルに値がある場合
if(Auth::user()->spending()->exists()) {
$spendings = Auth::user()->spending()->orderBy('date', 'DESC')->get();
}
// ---------- 日付検索による支出一覧表示 ----------
// 空で送信された時に、$fromと$untilは定義すらされていないのでエラーになってしまう。
// そのため事前に空の文字列を定義しておく(配列だとエラーになる)
$from = '';
$until = '';
//日付が選択されたら
// from until 両方選択された場合
if (!empty($request['from']) && !empty($request['until'])) {
$from = $request['from'];
$until = $request['until'];
$spendings = Auth::user()->spending()->wherebetween('date', [$from, $until])->get();
// from 選択された場合
} elseif ($request['from']) {
$from = $request['from'];
$spendings = Auth::user()->spending()->where('date', '>=', $from )->orderBy('date', 'DESC')->get();
// until 選択された場合
} elseif ($request['until']) {
$until = $request['until'];
$spendings = Auth::user()->spending()->where('date', '<=', $until )->orderBy('date', 'DESC')->get();
// from until 両方選択されなかった場合
} else {
$spendings = Auth::user()->spending()->orderBy('date', 'DESC')->get();
}
// ---------- 共通処理 ----------
// ----- リターン -----
return view('original.search.index', [
// 日付
"from" => $from,
"until" => $until,
// 「支出一覧表示」 or 「日付検索による支出一覧表示」の$spendings
// 「支出一覧表示」 = 日付選択がない場合
// 「日付検索による支出一覧表示」 = 日付選択がある場合
'spendings' => $spendings,
]);
// ---------- 共通処理 ----------
}
③ビュー
検索前:views/original/index.blade.php
↓検索
検索後:views/original/search/index.blade.php
ページのコードは検索前後で同じ
views/original/search/index.blade.php
<!-- 検索 -->
<section class="navbar justify-content-around">
// ⭐️getメソッドでページを遷移して、その間にコントローラーで処理し、表示させる
<form class="date_option" action="{{ route('search.index') }}" method="GET">
@csrf
// ⭐️検索した内容をページ更新後にvalueで表示
<input type="date" name="from" placeholder="from_date" class="cursor date_border" @if(isset($from)) value="{{$from}}" @endif>
<span class="mx-1 text-grey">~</span>
// ⭐️検索した内容をページ更新後にvalueで表示
<input type="date" name="until" placeholder="until_date" class="cursor date_border" @if(isset($until)) value="{{$until}}" @endif>
<button type="submit" class="btn btn-primary ml-3 search-btn">検索</button>
</form>
</section>
<!-- 一覧表示 -->
<section class="navbar mt-2">
<table class="table border">
<thead>
<tr class="card-header">
<th scope="col" class="text-center align-middle w-25">日付</th>
<th scope="col" class="text-center align-middle w-25">タイトル</th>
<th scope="col" class="text-center align-middle w-25">支出</th>
<th scope="col" class="text-center align-middle w-25"></th>
</tr>
</thead>
@if(Auth::user()->budget()->exists())
<tbody id="gallery">
@foreach ($spendings as $spending)
<tr class="card-body bg-white gallery">
<td class="text-center align-middle w-25">{{ $spending['date'] }}</td>
<td class="text-center align-middle w-25">{{ $spending['title'] }}</td>
<td class="text-center align-middle w-25">{{ number_format($spending['amount']) }}円</td>
<td class="text-center align-middle w-25">
<a href="{{ route('spending.show', ['spending' => $spending['id']]) }}" class="btn btn-info text-white btn-responsive">詳細</a>
</td>
</tr>
@endforeach
</tbody>
@endif
</table>
</section>