[英]Laravel: Redirecting to same route after filtering data
我有一個索引頁面,其中列出了我擁有的所有數據,還有一個簡單的過濾表單,該表單向其給定的路由發出POST
請求
@section('content')
<h4>Maçlar</h4>
<form class="form-inline" method="post" action="{{ route('games.filter') }}">
{{ csrf_field() }}
<div class="form-group mb-2">
<label class="sr-only">Email</label>
<select class="form-control" id="season-select" name="season">
<option value="0">Tüm Sezonlar</option>
@foreach ($seasons as $season)
<option value="{{$season->id}}" {{old('season') == $season->id ? 'selected' : ''}}>{{$season->season}}</option>
@endforeach
</select>
</div>
<div class="form-group mx-sm-3 mb-2">
<label for="week-select" class="sr-only">Password</label>
<select class="form-control" id="week-select" name="week">
<option value="0">Tüm Haftalar</option>
@foreach ($weeks as $week)
<option value="{{$week->id}}" {{old('week') == $week->id ? 'selected' : ''}}>{{$week->week}}</option>
@endforeach
</select>
</div>
<button type="submit" class="btn btn-primary mb-2">Filtrele</button>
</form>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Sezon</th>
<th scope="col">Hafta</th>
<th scope="col">Ev Sahibi Takım</th>
<th scope="col">Misafir Takım</th>
<th scope="col">Tarih ve Saat</th>
<th scope="col">Yer</th>
</tr>
</thead>
<tbody>
@foreach ($games as $game)
<tr>
<td>{{$game->season->season}}</td>
<td>{{$game->week->week}}</td>
<td>{{@$game->homeTeam->name}}</td>
<td>{{@$game->awayTeam->name}}</td>
<td>{{@$game->game_date_time}}</td>
<td>{{@$game->place}}</td>
</tr>
@endforeach
</tbody>
</table>
@endsection
在控制器中,我的索引方法如下
public function index()
{
$seasons = Season::all();
$weeks = Week::all();
$games = Game::with('season', 'week', 'homeTeam', 'awayTeam')->get();
return view('game.index')->with(compact('games', 'seasons', 'weeks'));
}
還有我的過濾器方法,該方法執行POST
請求並將經過過濾的數據傳遞到與索引使用相同的視圖。
public function filter(Request $request)
{
$seasons = Season::all();
$weeks = Week::all();
$games = Game::with('season', 'week', 'homeTeam', 'awayTeam')
->when(request('season') != 0, function ($q) {
return $q->where('season_id', request('season'));
})->when(request('week') != 0, function ($q) {
return $q->where('week_id', request('week'));
})
->get();
return view('game.index')->with(compact('games', 'seasons', 'weeks'));
}
我想學習的是,實現這種情況的最佳方法是什么? 執行POST
請求進行過濾后,是否可以重定向回相同的索引路由? 而且old
方法在刀片模板中不起作用,因此請求后我無法顯示舊表單數據。 這可能是什么問題? 最后,如何在索引和過濾器方法中刪除這些重復的行。
$seasons = Season::all();
$weeks = Week::all();
任何幫助,將不勝感激。 PS:我不想使用Ajax,Vue.js等。我想在Laravel中使用它。
您也可以在索引方法中使用過濾器。 無需創建其他過濾方法。
改變看法。
<form class="form-inline" method="post" action="{{ route('games.index') }}"> // form redirect to index method
更改索引方法。
public function index(Request $request)
{
$seasons = Season::all();
$weeks = Week::all();
if($request->has('season') && $request->has('week') ){ // check filter value is passed or not
$games = Game::with('season', 'week', 'homeTeam', 'awayTeam')
->when(request('season') != 0, function ($q) {
return $q->where('season_id', request('season'));
})->when(request('week') != 0, function ($q) {
return $q->where('week_id', request('week'));
})
->get();
}
else{
$games = Game::with('season', 'week', 'homeTeam', 'awayTeam')->get();
}
return view('game.index')->with(compact('games', 'seasons', 'weeks'));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.