代码之家  ›  专栏  ›  技术社区  ›  Tartar

Laravel:过滤数据后重定向到同一个路由

  •  0
  • Tartar  · 技术社区  · 6 年前

    我有一个索引页,列出了我所拥有的所有数据,还有一个简单的筛选表单,它使 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'));
    }
    

    还有我的过滤方法 邮政 请求并将筛选后的数据传递给索引使用的同一视图。

    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'));
    }
    

    我想了解的是,实现这种情况的最佳方法是什么?执行后是否可以重定向回相同的索引路由 邮政 请求筛选?和 old 方法在刀刃模板中不起作用,因此在请求后无法显示旧表单数据。有什么问题吗?最后,如何删除索引和筛选方法中的这些重复行。

    $seasons = Season::all();
    $weeks = Week::all();
    

    任何帮助都将不胜感激。PS:我不想使用Ajax、Vue.js等,我想用Laravel来实现。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Yasin Patel    6 年前

    您也可以在索引方法中使用过滤器。不需要为筛选器创建其他方法。

    在视图中更改。

    <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'));
    }