Laravel pagination redirects to home route after clicked on link for page 2

1.1k Views Asked by At

My Laravel pagination is not working properly. When I try to click on second link in laravel pagination or another one instead of first, it redirects me to home url (localhost:8000).

Laravel pagination links are redirecting to same route but with "page=" parameter but the problem is that on this route I have controller with if statements which redirects me to home page. How can I send my pagination results to another route and display every pagination page properly without get affected by controllers.

Here is controller:

    $per_page = 5; //default

    //Za ispis kategorija i tagova u filteru
    $filter_categories = Category::all();
    $filter_tags = Tag::all();


    if($request->per_page){ $per_page = $request->per_page; }


    //Ako ne postoji zahtjev za kategorijama i tagovima
    if(!$request->category && !$request->tags) {

        //Provjera da li postoji zahtjev za broj jela po stranici
        if(only_meals_per_page($request)){

            $meals = Meal::paginate($per_page);

            return view('index', compact('meals', 'filter_categories', 'filter_tags'));
        }

        //Ako ne postoji nikakvi zahtjev vrati se na početak
        return redirect('/');

    //Zahtjev za kategorijom
    } else if($request->category && !$request->tags) {

        $result = filter_meals_by_category($request->category, $per_page);

    //Zahtjev za tagovima
    } else if(!$request->category && $request->tags) {

        $result = filter_meals_by_tags($request->tags, $per_page);

    //Zahtjev za kategorijama i tagovima
    } else {

        $result = filter_meals_by_categories_and_tags($request->category, $request->tags, $per_page);

    }

    $meals = $result['meals'];
    $message = $result['message'];

    Session::flash('message', $message);
    return view('index', compact('meals', 'filter_categories', 'filter_tags'));

..and example of $result array with pagination metod

$meals = Meal::where('category_id','=', NULL)->paginate($per_page);

I know that the problem is probably in first if statement which redirects me to home because there were no categories and tags in request anymore.

1

There are 1 best solutions below

0
On

Problem fixed with appending query string of get request in pagination links:

$meals->appends(request()->input())->links()