Product Latest View返回空数组



我想在详细信息页面中显示最近查看的产品。我创建了一个表:

Schema::create('recently_viewed_products', function (Blueprint $table) {
$table->id();
$table->integer('product_id');
$table->string('session_id');
$table->timestamps();
});

在我的IndexController:

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

如果点击一个产品,我将在最近查看的产品表中获得产品id和sessionid,但我无法在产品详细信息页面中显示。我得到一个空数组

我想问题出在其中:

$recentlyViewedProducts = Product::whereIn('id',$recentProductIds)->get()->toArray();

我尝试在产品详细信息页面显示我点击的产品

//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');

DB中的字母b是小写的,你应该把它改成大写

最新更新