Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lara_Love's avatar

redirect to home not work

Sorry, maybe I explained it wrong. We need the user's ID, so if the user is not logged in and clicks on link 1, he will first be directed to the home page and logged in, then come back and click on link 1

Route: this link save product into session and iwe want before save in session redirect us to Page home

 Route::get('add-to-cart/{id}', [ProductController::class, 'addToCart'])->name('add.to.cart')->middleware('logined');

kernel.php

'logined' => \App\Http\Middleware\Login::class,

Middleware Login

public function handle(Request $request, Closure $next) {
        if (Auth::check()) {
            return $next($request);
        }
        redirect()->route('home');
    }

function store product

public function ad(Request $request)
    {
            $products = $request->session()->get('cart');
            $uuid = str()->uuid();
            foreach ($products as $product) {
                Faktor::create([
                    'uuid' => $uuid,
                    'user_id' => Auth::id(),
                    'product_id' => $product['product_id'],
                    'name' => $product['name'],
                    'quantity' => $product['quantity'],
                    'price' => $product['price'],
                ]);
            }
        return redirect()

I hope I have explained correctly

0 likes
14 replies
vincent15000's avatar

Can you please write your entire code between 3 backticks so that it's more readable ?

tykus's avatar

@lovercode you can simply use the auth middleware here to protect the route:

 Route::get('add-to-cart/{id}', [ProductController::class, 'addToCart'])
    ->name('add.to.cart')
    ->middleware('auth');

When the user is authenticated (after begin redirected to login); (s)he will be automatically redirected to the intended original URL (see the AuthenticatedSessionController).

Aside, you are using a GET Request to add an item to the Cart; this should be POST (by convention).

2 likes
Lara_Love's avatar

@tykus i use

Route::get('add-to-cart/{id}', [ProductController::class, 'addToCart'])
    ->name('add.to.cart')
    ->middleware('auth');

but it dosn't work .

1 like
tykus's avatar

@LoverCode explain what it doesn't work means; what action do you take, and what is happening that is unwanted/unexpected?

2 likes
Lara_Love's avatar

@tykus we need user_id if user click on PAY btn https://postimg.cc/rzhD1ttp and he dosn't login we should redirect user to login page that we can get user_id for invoice

<a href="{{ route('add.to.cart', $product->id) }}" >PAY </a>
1 like
tykus's avatar
tykus
Best Answer
Level 104

@LoverCode I understand the concept, but you don't explain what not working means - what is actually happening when you click the Pay button; are you redirected to the home route; do you sign in; what happens then? Also, where is the Route definition for home, can you show it? Have you reverted to using the logined middleware again?

JussiMannisto's avatar

You're missing a return statement in your middleware. Instead of this:

redirect()->route('home');

Use this:

return redirect()->route('home');
2 likes
Lumethys's avatar

are you using custom authentication? because if you use Breeze or Jetstream, add the auth middleware will automatically do this for you

route file:

 Route::get(
   'add-to-cart/{id}',
   [ProductController::class, 'addToCart'],
  )
  ->name('add.to.cart')
  ->middleware('auth');

that's it

Please or to participate in this conversation.