Home > OS >  How to read data from Controller action redirect?
How to read data from Controller action redirect?

Time:08-19

The docs show how to redirect to a controller action:

return redirect()->action(
    [HomeController::class, 'index'], ['id' => 1]
);

but how do I read the id parameter inside HomeController?

These don't seem to work:

1

class HomeController extends Controller
{
    public function index(Request $request)
    {
        dd($request);
    }
}

2

class HomeController extends Controller
{
    public function index($id)
    {
        dd($id);
    }
}

3

class HomeController extends Controller
{
    public function index(Action $id)
    {
        dd($id);
    }
}

CodePudding user response:

class HomeController extends Controller
{
    public function index(Request $request, $id)
    {
        dd($id);
    }

    public function update(Request $request, $id)
    {
        //Do something

        return redirect()->action(
           [HomeController::class, 'index'], ['id' => 1]
        );
    }
}

In web.php

use App\Http\Controllers\HomeController;

Route::get('/{id}', [HomeController::class, 'index']);

CodePudding user response:

If your route doesn't take a route parameter then calling action will append id as a query string parameter. This would be an input on the request which you can access via the Request object as an input:

$request->input('id')
  • Related