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

jericopulvera's avatar

Is it possible to store query on session?

$query = Property::with(['booking','photos','attributes'])->whereHas('booking', function ($q) use ($request, $check_in, $check_out) {
                $q->where('arrival', '!=', $check_in)
                ->where('departure', '!=', $check_out);
            })->where('status', 'Active');

   
        session()->forget('query');
        session()->put('query', $query);
        
        $properties = $query->paginate(15);

       return view('properties', compact('properties'));
$query = Session::get('query');
$query->whereHas('attribute', function ($q) {
    $q->where('name', 'something');
});

dd($query);

Tried this but it returns null.

0 likes
5 replies
topvillas's avatar

Try using json_encode($query) before storing it in the session and json_decode($query) when pulling it out. You'll need to use collect($query) if you want to turn it back into a Collection though.

martinbean's avatar

@Eco012390 Why would you want to? Just store an identifier instead.

You don’t want to be exposing things like your application’s database queries by storing it client-side in a user’s session. Also, what happens if you store a query in a user’s session, but then you deploy a new version of your application that changes the query? Your application’s probably going to break when reading the query back.

jericopulvera's avatar

@martinbean I'm doing a search and filtering functionality that uses ajax in filters.

I would like to pass the query so that I can retrieve it and add the filter queries on my endpoint for filtering in ajax.

I have thought of storing only the search inputs to the session and query it and write a query again with the filters. but I thought it would look dirty that's I wanted to pass the query.

if it's a bad idea, can you suggest a better clean solution for my problem.

martinbean's avatar

@Eco012390 Why do you need to store anything in a session for filtering? Just pass the options the user selected to an endpoint, and filter in there.

As I say, you should not be storing a raw SQL query anywhere near the client side. This exposes your application’s inner workings and gives would-be hackers more insight to effectively exploit your application.

1 like
Snapey's avatar
Snapey
Best Answer
Level 122

You could store the filters on the session (session data is stored server side), however, the following can lead to poor user experience.

  1. The user opens a duplicate tab. Any variance in the search in that tab will impact the first tab since they are both the same session.

  2. The user cannot bookmark what got them to that set of results.

Convention is to hold this sort of state in the URI - for good reasons.

I have made the above mistake myself, so speaking from experience. I have a live site where you can see this in action. Try it out at https://speakernet.co.uk (i'm not aiming to plug the site, just so you can see the effect)

Click on Browse Talks, then select some combination in the right hand panel, e.g. Category 'history' then search. The combination of filters is stored in session.

Now duplicate the tab and in the new tab choose a different category, e.g. 'Health' - one page of results is shown. Health is now in session.

Now switch to the first tab and at the bottom choose a different page (paging through the results). You will see that there are no results and the category on this tab has changed to Health.

1 like

Please or to participate in this conversation.