Support the ongoing development of Laravel.io →
posted 11 years ago
Requests

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?

Last updated 3 years ago.
0

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

Last updated 3 years ago.
0

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 ;)

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

heidilux heidilux Joined 3 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.