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

AlexSteele's avatar

is there a better way to set a cookie and pass values to db after a login?

I would like to merge a table where I change some of the 'line items' in this quote. for most cases, where someone had browsed on a computer, I provide a method to register and quote with one button. works great! but what about a scenario where someone has an account, comes back. does not sign in immediately, puts items in the quote list, and then decides to login. their old login had maybe 3 items, whatever is new should be merged in. So I was trying some interesting things, because the 'ghost' user line item id is set to be 1 (that's how I know its a guest not logged in that just used a print function to make a list). the line item state is open, because we didn't close the line item by making a quote yet, cookies were used because they are a guest (not used when signed in) the ip is captured, and then we note the cookie - which is set as :

$kindaCooky = session('_token');

The issue is that I am apparently using the wrong thing to set a cookie (as a session) because after login, that variable changes, which makes blending the guest quote list with an authenticated quote list tough, as this filter yields no results after a login.

$lineitems = Lineitem::where('user_id', '=', 1) ->where('state', '=', 'open') ->where('used_cookie', '=', 1) ->where('ip_address', '=', $_SERVER['REMOTE_ADDR']) ->where('cookie_value', '=', $kindaCooky) ->get();

How should I be getting this cookie? I need a cookie that will not change after a login. and I know that the remote server address is not 100 percent reliable, and this filter would do better to leave it out.

0 likes
3 replies
rodrigo.pedra's avatar

You can try using a middleware for

You would add this middleware just to your login route (the POST one, which actually logs the user in, not the one which shows the view) save any data you need from the session before proceeding, and then merging the quotes if the login was successful.

Something like this:

<?php

namespace App\Http\Middleware;

use App\Models\User;
use Illuminate\Http\Request;

class MergeQuotes
{
    public function handle(Request $request, \Closure $next)
    {
        // before procssing the request, we grab the current quotes
        $quotes = $request->session()->get(/* ... */);

        // we tell laravel to process the request
        $response = $next($request);

        // if the response is successful, and the request now
        // has a user, we merge the quotes
        if ($response->isSuccessful() && $request->user()) {
            $this->mergeQuotes($request->user(), $quotes);
        }

        return $response;
    }

    private function mergeQuotes(User $user, $quotes)
    {
        // your quote merging logic goes here
    }
}
1 like
rodrigo.pedra's avatar

Depending on what your using for authentication (laravel/ui, breeze, jetsream, custom auth, ...) you may need to check for the user using the auth() helper instead:

if ($response->isSuccessful() && auth()->check()) {
    $this->mergeQuotes(auth()->user(), $quotes);
}

In any login methods, the user should be logged in and set to the auth guard after the login is successful.

1 like
AlexSteele's avatar

Hello! I am going to post my solution here in case it helps someone. I revised the way these basket line items were being assigned. Dropped out the IP address, because this is not really reliable. and I set a cookie when the line item is added, and write that value to a field in the table. after logging in, rather than using middleware, I route to a greeting page where I filter on the line item status and the cookie value when it was set, and then assign the found set of records to the current user id. I like this because it appears that the middleware was constantly triggering and its too much, this only needs to trigger once. I am not holding a lot of information in the session, I am using a table approach to hold line items.

Please or to participate in this conversation.