Search code examples
phplaravelcontrollerstripe-paymentse-commerce

Check if $request is empty or not in laravel


I have a user detail page when he'll add his details than he can go to checkout page to enter card details.

Routes for detail page

Route::get('/checkout', 'CheckoutController@index')->name('checkout.index')->middleware('auth');
Route::post('/checkout', 'CheckoutController@store')->name('checkout.store');

When he'll add these information, he can access to payment page like this:

Route::get('/checkout/payment', 'CheckoutController@payment')->name('checkout.payment');
Route::post('/checkout/payment', 'CheckoutController@paymentstore')->name('payment.store');

I want to redirect user to /checkout page if user try to access directly to /checkout/payment without adding information.

My Controller

public function payment() {
    if(Cart::instance('default')->count() > 0) {
        if($request) {
            return view('checkoutpayment');
        }

        return redirect()->route('Checkout.index')
            ->withError('Please add your personal details to complete the checkout.');
    }

    return redirect()->route('cart.index')
        ->withError('You have nothing in your cart, please add some products first');
  }

Solution

  • $request only available for single request so you cannot get data on the payment page. But session definitely helps you so first of all you have to store your user data in session using $request->session() and then you have to check whether data is available on the payment page or not using $request->session()->has() method.