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.
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.
You could store the filters on the session (session data is stored server side), however, the following can lead to poor user experience.
-
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.
-
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.
Please or to participate in this conversation.