Jimsrico
Jimsrico

Reputation: 21

How to call a route from controller in laravel?

TestController

public function index()
{
    $data = Users::all();
    return view('welcome', compact('data'));
}

Routes

Route::get('/', 'TestController@index');

How can i call that route in my controller? I want to return route from index of my controller not the view.

Upvotes: 2

Views: 3189

Answers (2)

Kanin Peanviriyakulkit
Kanin Peanviriyakulkit

Reputation: 520

You can use

return redirect()->route('/');

ref: https://laravel.com/docs/5.1/responses#redirecting-named-routes

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

Use redirect to route:

return redirect()->route('mainpage');

And it's also a good idea to add a name to a root route:

Route::get('/', 'TestController@index')->name('mainpage');

Upvotes: 1

Related Questions