Laravel Redirect to Route from Controller Example
Hello Artisan,
This tutorial is focused on laravel redirect to route from controller. you will learn laravel redirect from route name. In this article, we will implement a how to redirect to another route in laravel. This post will give you a simple example of how to redirect route in laravel controller. Let's get started with how to return route in laravel.
You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.
We mostly need to redirect the route from the controller method in the laravel project. Laravel provides several ways to return redirect with route name in laravel. I will give you three ways to return a redirect to a specific route from the controller function. we will use redirect(), route() and to_route() functions for redirect to route. you need to pass the route name as an argument.
You can see below example code:
Demo Routes:
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 redirect() with route()
Controller File:
<?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 to_route()
Controller File:
<?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 redirect()
Controller File:
<?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");
}
}
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Use Google Translator in Laravel?
- Laravel Call Function from Same Controller Example
- Laravel Collection Check If Key Exists Example
- How to Convert Collection to JSON in Laravel?
- How to Generate App Key in Laravel?
- How to Select Specific Columns in Laravel Eloquent Model?
- How to Get All Models in Laravel?
- How to Get Columns Names from Model in Laravel?
- How to Create Model in Laravel using Command?
- Laravel Ajax PUT Request Example Tutorial
- Laravel Repository Pattern Tutorial Example
- How to Resolve Laravel Blank Page on Server?