mardi 22 mai 2018

Laravel: Redirecting to same route after filtering data

I have an index page where I list all the data that I have and there is also a simple filtering form that makes a POST request to its given route

@section('content')

    <h4>Maçlar</h4>

    <form class="form-inline" method="post" action="">
        
        <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="" ></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="" ></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></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        @endforeach
        </tbody>
    </table>

@endsection

And in the controller my index method is as below

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

And there is also my filter method which performs POST request and passes filtered data to same view as index uses.

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

What I want to learn is, what is the best way of implementing such a case ? Is it possible to redirect back to same index route after performing POST request for filtering ? And old method does not work in the blade template so I can not display old form data after the request. What can be the problem with it ? Lastly, how can I remove these duplicate rows in the index and filter methods.

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

Any help would be appreciated. PS: I do not want to use Ajax, Vue.js etc. I want to do it with Laravel.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2rZk62h
via IFTTT

Aucun commentaire:

Enregistrer un commentaire