Redirect route with two parameters in WITH [Laravel]

25,896

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' => '...'));
Share:
25,896
user3809590
Author by

user3809590

Updated on December 14, 2020

Comments

  • user3809590
    user3809590 over 3 years

    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
    
  • Pankaj
    Pankaj about 8 years
    I checked the first part ->with('cartSuccess', 'You successfuly ordered.) Can you explain how will I access the cartSuccess key in View?
  • The Alpha
    The Alpha about 8 years
    When redirecting, the with method will store the data using the key in the session so you can access the key for the vaalue from the session using Session::get('cartSuccess').
  • Pankaj
    Pankaj about 8 years
    is there any other less expensive way to do this, I don't want to store this kind of info in session. Can you please guide?
  • Yousef Altaf
    Yousef Altaf over 4 years
    @TheAlpha return Redirect::route('cart-success', array('cartSuccess' => '...', 'cartItems' => '...')); Now how to access this key in view cartSuccess I try with Session::get('cartSuccess') but noting coming back.
  • The Alpha
    The Alpha over 4 years
    @YousefAltaf, yes
  • Yousef Altaf
    Yousef Altaf over 4 years
    @TheAlpha thanks I got it, in this case I should use foreach.