我想在详细信息页中显示最近查看的产品。我创建了一个表:
Schema::create('recently_viewed_products', function (Blueprint $table) {
$table->id();
$table->integer('product_id');
$table->string('session_id');
$table->timestamps();
});
在我的索引控制器中:
public function ProductDetails($id){ //display the product page detail
$product = Product::findOrFail($id);
$multiImg = MultiImg::where('product_id',$id)->get();
// Set Session for Recentlty Views Products
if(empty(Session::get('seasion_id'))){
$session_id = md5(uniqid(rand(), true));
} else {
$session_id = Session::get('session_id');
}
Session::put('session_id',$session_id);
// Insert product in table if not already exists
$countRecentlyViewedProducts = DB::table('recently_viewed_products')
->where(['product_id'=>$id,'session_id'=>$session_id])->count();
if($countRecentlyViewedProducts==0){
DB::table('recently_viewed_products')->insert(['product_id'=>$id,'session_id'=>$session_id]);
}
//Get Recently Views Prodcuts Ids
$recentProductIds = Db::table('recently_viewed_products')->select('product_id')
->where('product_id','!=',$id)->where('session_id',$session_id)->inRandomOrder()->get()
->take(4)->pluck('product_id');
//Get Recently Views Prodcuts
$recentlyViewedProducts = Product::whereIn('id',$recentProductIds)->get()->toArray();
//dd($recentProductIds);
retur nview('frontend.product.product_details',compact('product','multiImg','recentlyViewedProducts','recentProductIds'));
} // end method
如果点击一个产品,我会在recentlyviewed*products表中得到产品 *id和会话id,但我无法在产品详细信息页面中显示。我只得到一个空数组。
我想问题出在哪里:
$recentlyViewedProducts = Product::whereIn('id',$recentProductIds)->get()->toArray();
我尝试在产品详细信息页面上显示我单击的产品
1条答案
按热度按时间5ssjco0h1#
DB
中的字母B为小写,应将其更改为大写