Otis
Otis

Reputation: 401

How to redirect to a post route

i want to be redirected to a post route using the redirect instead of a get.

My code below

public function resolve_booking(Request $request){
    $request->session()->put('paymode',request('paymode'));

    if(request('paymode') == 'Pay Online'){
        return redirect()->route('payment_online');
    }else{
        return redirect()->route('payment_delivery');
    }
}

Web.php

Route::post('/payment_online',[App\Http\Controllers\MyMessengerController::class, 'payment_online'])->name('payment_online')->middleware('auth');

Route::post('/payment_delivery',[App\Http\Controllers\MyMessengerController::class, 'payment_delivery'])->name('payment_delivery')->middleware('auth');

As you can see it's a post route and the redirect uses a get route, how can i make the redirect use the post route

Upvotes: 1

Views: 1386

Answers (2)

Rafah AL-Mounajed
Rafah AL-Mounajed

Reputation: 307

According to what I understand, you want to complete the process through another function that deals with the same request sent depending on a condition

If so you can simply call the method and pass the request.

Suppose you have a PaymentController and it has three functions

1. payment_online method

public function payment_online(Request $request){
  // some code
  return view('view1');
}

2. payment_delivery method

public function payment_delivery (Request $request){
  // some code
  return view('view2');
}

you can simply do this in 3. resolve_booking method :

public function resolve_booking(Request $request){
    $request->session()->put('paymode',request('paymode'));

    if(request('paymode') == 'Pay Online'){
        return $this->payment_online($request);
    }else{
        return  $this->payment_delivery($request);
    }
}

Upvotes: 2

Fireflies
Fireflies

Reputation: 77

From my experience, if you want to redirect to POST route, there is either some logic error in your code or you using POST incorrectly. However, if you really need to do it, this answer might give you some insights.

Upvotes: 2

Related Questions