user3574492
user3574492

Reputation: 6435

Laravel 5 how to return to a POST request with validation errors

I have a POST route in Laravel:

Route::post('/quote-summary', 'QuoteController@quoteSummary')->name('quote-summary');

On the view for this route I have a form that submits to another route:

Route::post('/proceed-to-payment', 'QuoteController@proceedToPayment')->name('proceed-to-payment');

Here is my controller method for proceed-to-payment:

public function proceedToPayment(Request $request) {

    if(empty($request->get('tick_statement_of_fact'))) {
        return redirect()->route('quote-summary')->with('tick_statement_of_fact', 'I am so frustrated.');


    } else {
        return view('quote.payment', compact('date_cover_required', 'expiry_date'));
    }

}

quoteSummary() method:

public function quoteSummary(QuoteRequest $request)
    {

        //dd($request);
        // get the quote
        $quote_data = $this->getQuote($request);
        $quote = number_format($quote_data, 2, '.', ',');



        if(!empty($request->get('units'))) {
            $units = $request->get('units');
        }
        if(!empty($request->get('limit_of_indemnity'))) {
            $limit_of_indemnity = number_format($request->get('limit_of_indemnity'), 2, '.', ',');
        }
        if(!empty($request->get('title'))) {
            $title = $request->get('title');
        }


        // store data in the session so we can access from generated documents
        session(['units' => $units, 'limit_of_indemnity' => $limit_of_indemnity, 'insured_name' => $insured_name, 'title' => $title, 'first_name' => $first_name, 'last_name' => $last_name, 'contact_number' => $contact_number, 'email' => $email, 'quote' => $quote]);



        return view('quote.summary');

    }

I am trying to redirect back to quote-summary but as it was a POST I am getting a MethodNotAllowedHttpException as the redirect seems to be doing a GET.

Any ideas on how I can return to my POST route with some validation error messages?

Upvotes: 1

Views: 2044

Answers (1)

Stas Solodkin
Stas Solodkin

Reputation: 31

Having both, post and get, routes for your quoteSummary action, would do just what you are after, i.e.:

Route::post('/quote-summary', 'QuoteController@quoteSummary')->name('quote-summary'); Route::get('/quote-summary', 'QuoteController@quoteSummary')->name('quote-summary');

Upvotes: 1

Related Questions