Hello, first time posting on here so please go gentle on me :) I've been asked to build a recently viewed product list into my ecommerce application - though I'm a little stumped as to how to go about integrating this.
Any suggestions or advice? I did think middleware might be an option...?
@devonian What’s the brief for the feature? If it’s just showing any one user the products they last viewed, then you can just keep an array in the session, and then retrieve those products in a “recently viewed” list.
class ProductController extends Controller
{
public function show(Product $product)
{
// Push product ID to session
session()->push('products.recently_viewed', $product->getKey());
return view('product.show', compact('product'));
}
}
class RecentlyViewedProductsViewComposer
{
public function compose(View $view)
{
$products = session()->get('products.recently_viewed');
$view->with([
'recentlyViewed' => Product::find($products),
]);
}
}