Recently Viewed

 avatar
debo2696
php_laravel_blade
2 years ago
3.1 kB
2
Indexable
Never
//Code for Recently viewed

//General trait:
function recently_viewed($request)
    {
        $p_id = Product::where('slug', $request->slug)->value('id');
        $rv = collect(['product_id' => $p_id, 'visitor_ip' => $request->ip()]);
        
        if($request->session()->has('user.id'))
        {
            $rv->put('user_id', $request->session()->get('user.id')); //un-explained
            $rv->user_id = $request->session()->get('user.id');
            $product_id_user_list = RecentlyViewed::where(['user_id'=> $rv->user_id])->pluck('product_id')->toArray();
        }
        else
        {
            $rv->put('user_id', null);
            $rv->user_id = null;
            $product_id_ip_list = RecentlyViewed::where(['visitor_ip'=> $request->ip(),'user_id'=> null])->pluck('product_id')->toArray(); 
        }
        $rv->visitor_ip = $request->ip();
        $rv->product_id = $p_id;

        if(isset($product_id_ip_list))
        {
            if(!in_array($p_id, $product_id_ip_list))
            {

                RecentlyViewed::Create($rv->all());
            }
            else
            {
                RecentlyViewed::where(['visitor_ip'=> $request->ip(), 'product_id'=> $p_id, 'user_id'=> null])->Update($rv->all());
            }
        }
        elseif(isset($product_id_user_list))
        {
            if(!in_array($p_id, $product_id_user_list))
            {

                RecentlyViewed::Create($rv->all());
            }
            else
            {
                RecentlyViewed::where(['product_id'=> $p_id, 'user_id'=> $rv->user_id])->Update($rv->all());
            }
        }
    }

//Homepge Controller
public function product_details(Request $request)
    {
        $data['pageheader'] = "Product Detail";
        $this->recently_viewed($request); //Adding to recently viewed table
        $data['product'] = Product::where('slug', $request->slug)->where('status', 'A')->first();
        // $data['types'] = DB::table('attribute_category_product')
        //     ->join('category_attribute', 'attribute_category_product.attribute_category_id', '=', 'category_attribute.id')
        //     ->join('products', 'attribute_category_product.product_id', '=', 'products.id')
        //     ->join('attributes', 'category_attribute.attribute_id', '=', 'attributes.id')
        //     ->where('attributes.title', 'Type')
        //     ->select('attribute_category_product.attribute_value')
        //     ->get();
        //     // ->pluck('attribute_category_product.attribute_value');
        // dd($data['types']);
        
        if($request->session()->has('user.id'))
            $data['recents'] = RecentlyViewed::where('user_id', $request->session()->get('user.id'))->orderBy('updated_at', 'DESC')->with('products')->take(10)->get();
        else
            $data['recents'] = RecentlyViewed::where(['visitor_ip'=> $request->ip(), 'user_id'=> null])->orderBy('updated_at', 'DESC')->with('products')->take(10)->get();
        //dd($data);
        return view('frontend.product-details', $data);
        //Dashboard 
    }