【问题标题】:Laravel: Redirecting to same route after filtering dataLaravel:过滤数据后重定向到相同的路由
【发布时间】:2018-10-31 23:17:09
【问题描述】:

我有一个索引页面,其中列出了我拥有的所有数据,还有一个简单的过滤表单,可以向其给定路由发出 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 做。

【问题讨论】:

    标签: php laravel laravel-5 laravel-5.6


    【解决方案1】:

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

    改变看法。

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

    【讨论】:

    • 我正在使用资源控制器,我认为商店功能已经使用了 games.index POST 路由。抱歉,我的问题中没有提到这一点。
    • 我的意思是有如下路线,所以我不能只使用游戏,因为它已经被商店使用了。还是我错了?邮政|游戏 |游戏商店
    • games.idex 也是一个 get 方法,但 form 的方法是 POST。效果如何?
    • 搜索表单大多是get,速度也很快。所以你可以使用 games.index 作为 GET
    猜你喜欢
    • 2014-04-12
    • 2020-03-24
    • 2020-10-10
    • 2018-11-08
    • 2017-06-19
    • 2021-04-26
    • 2020-12-14
    • 1970-01-01
    • 2018-06-08
    相关资源
    最近更新 更多