Hashim Aziz
Hashim Aziz

Reputation: 6092

Can't pass Request from controller to another controller's view

I'm trying to pass a Request from a controller, but for reasons I don't understand the data simply isn't being passed through to the view and I get Undefined variable: request. I have confirmed that right up until the redirect to the action the request is populated with all the additional variables, so the issue must be after that.

ManufacturerController

public function decode(Manufacturer $manufacturer, Request $request) {

    $validated = $request->validate([
    "id" => ["required","min:5","max:30", "alpha_num"],
    "email" => ["email","required","max:255"]
    ]);

    $request->merge([
        "manufacturer" => $manufacturer
    ]);

    // Pass the Request to the Manufacturer model and return a modified version of it
    $request = $manufacturer->oneplus($request); 

    return redirect()->action([TransactionController::class, "index"])->with($request);
    }

abort(404);

}

Manufacturer model:

public function oneplus($request) {

  $id = $request->id;

  /* BUSINESS LOGIC THAT GENERATES $new FROM $id... */
  
  $request->merge([
    'new' => $new
  ]);
      
  return $request;
}

Route in web.php

Route::get('/payment', [TransactionController::class, "index"]);

TransactionController:

public function index()
{
    return view('payment');
}

payment.blade.php

{{ dd($request->new) }}

Upvotes: 0

Views: 978

Answers (3)

apokryfos
apokryfos

Reputation: 40683

The problem when using redirects is that the redirect will cause a brand new request to happen. When using redirect()->with('variable', 'value') you need to then access that variable using:

session('variable')` 

the reason being that the variable is "flashed" to the next request via the session (in practice it's not sent to the next request, it's just available for the next request through the session and then disappears).

While this may be an easy solution to your problem a better solution is to not use a redirect if possible. Here's a simplification of an alternative:

ManufacturerController:

public function decode(Manufacturer $manufacturer, Request $request) {

    $validated = $request->validate([
    "id" => ["required","min:5","max:30", "alpha_num"],
    "email" => ["email","required","max:255"]
    ]);

    $request->merge([
        "manufacturer" => $manufacturer
    ]);

    // Pass the Request to the Manufacturer model and return a modified version of it
    $request = $manufacturer->oneplus($request); 
    $transactionController = app()->make(TransactionController::class);
    return $transactionController->index($request);
}

TransactionController:

public function index(Request $request)
{
    return view('payment')->with("request", $request);
}

This will call the other controller method within the same request.

Upvotes: 1

Nay Lin Aung
Nay Lin Aung

Reputation: 164

You can pass like this
ManufacturerController :

return redirect()->action(
    [TransactionController::class, "index"],
    ['data' => $request]
);

Route in web.php

// ? = Optional
Route::get('/payment/{data?}', [TransactionController::class, "index"]);

TransactionController:

public function index($data)
{
    return view('payment');
}

Upvotes: 1

Deepak
Deepak

Reputation: 598

You need to make few changes in TransactionController and ManufacturerController to make it work

TransactionController:

public function index(Request $request)
{
    return view('payment', [
        'request' => $request->session()->get('request')
     ]);
}

ManufacturerController:

public function decode(Manufacturer $manufacturer, Request $request) {

    $validated = $request->validate([
    "id" => ["required","min:5","max:30", "alpha_num"],
    "email" => ["email","required","max:255"]
    ]);

    $request->merge([
        "manufacturer" => $manufacturer
    ]);

    // Pass the Request to the Manufacturer model and return a modified version of it
    $request = $manufacturer->oneplus($request); 

    return redirect()->action([TransactionController::class, "index"])->with('request', $request->all());
    }

abort(404);

}

Upvotes: 1

Related Questions