amilajack
amilajack

Reputation: 127

Laravel not redirecting to route

I'm trying to validate some data from a form and redirect the user to the page with any errors if any. My code doesn't redirect to any route. The routing for all of my routes works correctly. I echoed the input with Input::all() and it does have the user input. The validator works as well. I'm not sure exactly what's preventing the Redirect::route from working

public function postPurchase()
{
    $validator = Validator::make(Input::all(), array(
        'condition' => 'required',
        'memory' => 'required',
        'color' => 'required',
        'accessories' => 'required',
        'shipping' => 'required'
    ));

    // $input = Input::all();
    // dd($input);

    if ($validator->fails()) {
        // echo "string";
        return Redirect::route('home');

    } else {
        echo "this succedded";
    }
    //Get prices, item id, etc and send user to checkout page
    // echo "Get prices, item id, etc and send user to checkout page";
}

This is the code that precede the postPurchase method:

public function getPurchase()
    {
        return View::make('general.purchase');
    }

    public function getCheckout()
    {
        return View::make('general.checkout');
    }

    public function postPurchaseCheck()
    {
        $input = Input::all();
        $this->input = $input;

        if (Input::get('buy')) {
            $this->postPurchase();
        }
        elseif (Input::get('cart')) {
            $this->postAddCart();
        }

    }

Upvotes: 1

Views: 105

Answers (3)

Aditya Lepcha
Aditya Lepcha

Reputation: 194

Try updating this

if (Input::get('buy')) {
            return $this->postPurchase();
 } elseif (Input::get('cart')) {
   return $this->postAddCart();
}

and

 if ($validator->fails()) {
        // echo "string";
        return Redirect::to('home');

    } else {
        echo "this succedded";
    }

also don't forget to define it on route file

Upvotes: 2

Arun Kumar
Arun Kumar

Reputation: 1198

Change you postPurchaseCheck() method to return what is returned by $this->postPurchase() like this. Because you are calling the postPurchase() method internally but post is happening on postPurchaseCheck() method so redirect has to be returned by the method that handle POST request

public function postPurchaseCheck()
{
    $input = Input::all();
    $this->input = $input;

    if (Input::get('buy')) {
       return $this->postPurchase();
    }
    elseif (Input::get('cart')) {
       return $this->postAddCart();
    }

}

Upvotes: 0

Laurence
Laurence

Reputation: 60068

You call the function - but you dont 'return' the Redirect that is given back to you.

Change

if (Input::get('buy')) {
            $this->postPurchase();
        }

to

if (Input::get('buy')) {
            return $this->postPurchase();
        }

Upvotes: 2

Related Questions