How To Redirect to Route From Controller In Laravel

  Feb 2024
  ITSolutionsGuides
  Category: Laravel
How To Redirect to Route From Controller In Laravel

Welcome To ITSolutionsGuides,


Lets see how to redirect from the controller and using the routes in laravel using simple functions like redirect() and redirect()->route() to more advanced techniques such as to_route(). After performing the functions in the controller we need to redirect the user to another page or we need to navigate the user to the another page. Whether you're aiming to redirect from a controller using the route name or dynamically redirecting to another route, we've got you covered. Therefore for redirecting using the routes laravel provides three different ways redirect(), redirect()->route(), to_route() . Lets see how to redirect from the controller using the routes in laravel using three different examples.

Let's Create Routes

Lets create the routes for the redirect example.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\UserController;
use App\Http\Controllers\HomeController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('users', [UserController::class, 'index']);
Route::get('home', [HomeController::class, 'index'])->name("home");

Example 1

Using the redirect() with route().

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, $page = 1)
    {
        $users = User::get();
  
        return redirect()->route("home");
    }
}

Example 2

Using the to_route().

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, $page = 1)
    {
        $users = User::get();
  
        return to_route("home");
    }
}

Example 3

Using the redirect() in the laravel.

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, $page = 1)
    {
        $users = User::get();
  
        return redirect("home");
    }
}

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides