Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect route with two parameters in WITH [Laravel]

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
like image 731
user3809590 Avatar asked Aug 23 '14 08:08

user3809590


People also ask

How do I redirect one page to another in laravel?

By default laravel will redirect a user to the home page like so: protected $redirectTo = '/home'; To change that default behaviour, add the following code in App/Http/Controllers/Auth/LoginController. php . This will redirect users (after login) to where ever you want.


1 Answers

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' => '...'));
like image 161
The Alpha Avatar answered Sep 21 '22 01:09

The Alpha