6
votes

I have problem to pass two varialbles with "with" in Redirect::route... Here is my code...

How to do this

return Redirect::route('cart-success')->with(
            array(
                'cartSuccess' => 'You successfuly ordered. To track your order processing check your email', 
                'cartItems' => Cart::contents()
            )
        );

Here is error:

Undefined variable: cartItems (View: C:\xampp\htdocs\laravel-webshop\laravel\app\views\cart-success.blade.php)

Route::group(array('before' => 'csrf'), function() {
    //Checkout user POST
    Route::post('/co-user', array(
        'as' => 'co-user-post',
        'uses' => 'CartController@postCoUser'
    ));
});

CONTROLLER

public function postCoUser() {
    $validator = Validator::make(Input::all(), array(
        'cardholdername' => 'required',
        'cardnumber' => 'required|min:16|max:16',
        'cvv' => 'required|min:3'
    ));

    if($validator->fails()) {
        return Redirect::route('checkout')
                ->withErrors($validator)
                ->withInput();
    } else {
        return Redirect::route('cart-success')->with(
            array(
                'cartSuccess' => 'You successfuly ordered. To track your order processing check your email', 
                'cartItems' => Cart::contents()
            )
        );
    }
}

View

 @extends('publicLayout.main')

 @section('content')
   @if(Session::has('cartSuccess'))
    <p>{{ Session::get('cartSuccess') }}</p>

    <?php $total = 0; ?>
    @foreach ($cartItems as $cartItem)
        Name: {{ $cartItem->name }} <br>
        Price: {{ $cartItem->price }} &euro;<br>
        Quantity: {{ $cartItem->quantity }} <br>
        <?php $final = $cartItem->price * $cartItem->quantity; ?>
        Final price: {{ $final }} &euro;<br>
        <?php $total += $final; ?>
        <hr>
    @endforeach
    Total: {{ $total }} &euro;
 @endif
@stop
3
Redirect::with() set the session variable, not setting your view variable. Where is the method defined in route cart-success? From there you'll have to retrieve the session value and pass the value to your view using View::make()JofryHS
I need session variable... I can get one session var but not two.. that is problemuser3809590
Where is route('cart-success') going? Can you post the code to that route as well (the controller method)?JofryHS
Still not showing where cart-success is? You are only showing your POST route, not the route after the submission (success route)JofryHS
I edited again... Is that what you need ?user3809590

3 Answers

10
votes

You may try this:

return Redirect::route('cart-success')
               ->with('cartSuccess', 'You successfuly ordered. To track your order processing check your email')
               ->with('cartItems', Cart::contents());

Or this:

return Redirect::route('cart-success', array('cartSuccess' => '...', 'cartItems' => '...'));
0
votes

You can pass two variables like that

$response=array('cartSuccess' => 'You have successfully ordered. To track your order processing check your email', 'cartItems' => Cart::contents());    

return Redirect::route('cart-success',$response);
0
votes

You can use this as well, it will help to keep the code format equal to usual route.

return \Redirect::route('your.route.name',['param1'=>'param1_value','param2'=>'param2_value']);