I'm trying to return recently viewed items, on the homepage, for the visitor based on their session. But i think because of the duplicate session query, even when it's not the database driver i'm using, the results from the session are always returned twice.
I asked in a related question about the duplicate session query. But it seems like there's not much i can do about that.
@bobbybouwmann's answered on a similar question about the duplicate session: "Anyway, you're not the only one with this issue. It seems to happen in the StartSession middleware and I don't think you can do anything about it".
Does anyone know how prevent the same result to appear twice?
In my controller
session()->push('deals.recently_viewed', $deal->getKey());
My complete controller
<?php
namespace App\Http\Controllers;
use App\Models\Deal;
use App\Filters\DealFilters;
use App\Models\Category;
use App\Models\City;
use App\Models\Address;
use Carbon\Carbon;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
class DealController
{
public function show($countrySlug, $citySlug, Category $category, Address $address, Deal $deal)
{
$city = City::where('country_slug', $countrySlug)->where('slug', $citySlug)->firstOrFail();
session()->push('deals.recently_viewed', $deal->getKey());
$mediaItems = $deal->getMedia('multi_collection');
$imagesPerChunk = count($mediaItems);
return view('deals.show', [
'city' => $city,
'category' => $category,
'address' => $address,
'deal' => $deal,
'mediaItems' => $mediaItems,
'imagesPerChunk' => $imagesPerChunk
]);
}
}
My composer
<?php
namespace App\Http\View\Composers;
use App\Models\Deal;
use Illuminate\View\View;
class InterestedComposer
{
public function compose(View $view)
{
$deals = session()->get('deals.recently_viewed');
$view->with([
'recentlyViewed' => Deal::find($deals),
]);
}
}
Blade
@foreach(Session::get('deals')['recently_viewed'] as $deals)
<span>{{ $deals }} </span>
@endforeach
The result when i visit a page is the id of that deal will be returned twice. That obviously not what i want.
4 4