I suspect that the answer here is a simple 'how to use laravel' kind of answer, but I can't seem to track down the answer...
I have a controller, for simplicity's sake, let's say it hash two methods... one to save some info to the database, and another to display return a view with some data along with it.
My pared down DB method. This works fine as far as saving the appropriate info to the DB.
public function saveOrder(User $user)
{
$newOrder = $this->saveNewOrder($order);
$cust = Stripe_Customer::retrieve($user->billing_id);
return Redirect::route('success', ['user' => $user, 'newOrder' => $newOrder, 'cust' => $cust]);
}
And what I'd like the 'success' route to behave like...
public function showSuccess(array $data)
{
$user = $data['user'];
$order = Order::find($data['newOrder']);
$cust = $data['cust'];
return View::make('shop.success', compact('order', 'user', 'cust'));
}
With this here, I'm getting an error: ErrorException Argument 1 passed to ShopController::showSuccess() must be of the type array, string given
. The 'data' being passed to the route seems to be just the User object rather than the array passed in the Redirect
.
The relevant routes are:
Route::get('/cart/success/{data}', array('as' => 'success', 'uses' => 'ShopController@showSuccess'));
Route::get('/cart/save-order/{email}', array('as' => 'saveOrder', 'uses' => 'ShopController@saveOrder'));
I also tried just calling the second method from within the first ( with a $this->showSuccess($data)
kind of thing) instead of return Redirect
... no go. Ended up with a blank page.
TL;DR
How do I pass an array of data from one method to another?
try this
public function saveOrder(User $user)
{
$newOrder = $this->saveNewOrder($order);
$cust = Stripe_Customer::retrieve($user->billing_id);
return Redirect::route( 'success' )
->with( 'user', $user )
->with( 'newOrder', $newOrder )
->with( 'cust', $cust );
}
public function showSuccess()
{
$user = Session:get( 'user' );
$order = Order::find( Session::get( 'newOrder' ) );
$cust = Session::get( 'cust' );
return View::make('shop.success', compact('order', 'user', 'cust'));
}
The only place where you can pass an array that it will explodes into single variables is on View::make
, Redirect::to
, Redirect::action
and Redirect::route
, etc, you must use the with
function, those values will be available in the Session
singleton
Also you can also use the with
function on View::make
Still I don't think this solution gonna work for you, because you are storing an object with Session
, you gonna to think a workaround for this problem
Thanks arcollector. You're right of course. That does work. I was hoping for a different solution, but that may be the most elegant available to me. I'm not sure why I was hoping for a different solution though ;)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community