Current File : //home/tradevaly/text.tradevaly.com.bd/app/Http/Controllers/ProductController.php
<?php

namespace App\Http\Controllers;

use App\Models\Banner;
use App\Models\BuyOffer;
use App\Models\Package;
use App\Models\FeaturedProduct;
use App\Models\ViewProduct;
use App\Models\Product;
use App\Models\User;
use App\Models\Tag;
use App\Models\Category;
use App\Models\Country;
use App\Models\Testimonial;
use App\Models\Tradeshows;
use Illuminate\Http\Request;
use Brian2694\Toastr\Facades\Toastr;
use Carbon\Carbon;
class ProductController extends Controller
{
     
     public function country()
    {
       
    }
     public function about_us ()
    {
       
    }
    public function view(Request $request, $slug)
    {
        $data['product'] = Product::where('slug', $slug)->firstorFail();
        Product::where('slug', $slug)->first()->increment('view');

        $all_view = ViewProduct::where('product_id',$data['product']->id)->where('ip_address',$request->getClientIp())->first();
        if ($all_view) {
            $all_view->expire_date =  Carbon::now()->addDays(7);
            $all_view->increment('view');
            $all_view->save();
        }else{
            $ViewProduct = new ViewProduct();
            $ViewProduct->product_id = $data['product']->id;
            $ViewProduct->ip_address = $request->getClientIp();
            $ViewProduct->expire_date =  Carbon::now()->addDays(7);
            $ViewProduct->view =  1;
            $ViewProduct->save();
        }

        $data['related'] = Product::where('supplier_id', $data['product']->supplier_id)->where('slug', '!=', $slug)->take(4)->get();
        return view('frontend.pages.single_product')->with($data);
    }
    
    public function buyers(){
        $data['country'] = Country::all();
        $data['buyoffer'] = BuyOffer::latest()->take(20)->get();
        $data['tags'] = Tag::orderBy('views', 'desc')->take(10)->get();
        $data['tagsbyword'] = Tag::orderBy('views', 'desc')->get();
       return view('frontend.pages.buyers')->with($data); 
    }
    
    public function manufacturers(){
        $data['country'] = Country::all();
        $data['buyoffer'] = BuyOffer::latest()->take(20)->get();
        $data['tags'] = Tag::orderBy('views', 'desc')->take(10)->get();
        $data['tagsbyword'] = Tag::orderBy('views', 'desc')->get();
        $data['category'] = Category::where('parent_id', 0)->get();
       return view('frontend.pages.manufacturers')->with($data); 
    }
    
    public function products(){
        $data['category'] = Category::where('parent_id', 0)->get();
       return view('frontend.pages.products')->with($data); 
    }
    
    public function companies(){
        $data['seller'] = User::where('type', 'seller')->paginate(12);
       return view('frontend.pages.companies')->with($data); 
    }
    
     public function alcompanies(Request $request, $a){
        $data['seller'] = User::where('type', 'seller')->whereHas('company', function($q) use($a){
        $q->where('name', 'like',  $a . '%');
        })->paginate(12);
       return view('frontend.pages.companies')->with($data); 
    }
    
    public function contact()
    {
        $banners= Banner::where('status', 1)->take('3')->get();
        return view('frontend.pages.index')->with(compact('banners'));
    }
    
    public function categories(Request $request,$slug)
    {
        
        $data['cat'] =  Category::where('slug', $slug)->first();
        if($data['cat']->parent_id == 0){
        $data['categories'] = Category::where('parent_id', $data['cat']->id)->get();
        
            $tree = Array();
        if (!empty($parent)) {
            $tree = Category::where('parent_id', $parent)->pluck('id')->toArray();
            foreach ($tree as $key => $val) {
                $ids = self::getChildren($val);
                if(!empty($ids)){
                    if(count($ids)>0) $tree = array_merge($tree, $ids);
                }
            }
        }
       
       $data['tree'] = $tree;
       $data['buyoffer'] = BuyOffer::whereIn('category_id', $tree)->orWhere('category_id', $data['cat']->id)->take(30)->get();
       $data['products'] = Product::whereIn('category_id', $tree)->orWhere('category_id', $data['cat']->id)->take(12)->get();
        $data['tags'] = Tag::whereIn('category_id', $tree)->orWhere('category_id', $data['cat']->id)->orderBy('views', 'desc')->take(20)->get();
        return view('frontend.pages.main_categories')->with($data);
        } else {
           $subcat = Category::where('parent_id', $data['cat']->id)->pluck('id')->toArray();
           $childcat = Category::whereIn('parent_id', $subcat)->pluck('id')->toArray();
           $catid[] = $data['cat']->id;
           $catlist = array_merge($subcat,$childcat,$catid);
           $data['catlist'] = $catlist;
           
           $data['country'] = Product::whereIn('category_id', $catlist)->groupBy('country_id')->pluck('country_id')->toArray();
           $data['port'] = Product::whereIn('category_id', $catlist)->groupBy('port')->pluck('port')->toArray();
           $data['supplier'] = Product::whereIn('category_id', $catlist)->groupBy('supplier_id')->pluck('supplier_id')->toArray();
           $data['unit'] = Product::whereIn('category_id', $catlist)->groupBy('unit')->pluck('unit')->toArray();
           $data['port'] = Product::whereIn('category_id', $catlist)->groupBy('port')->pluck('port')->toArray();
           $data['time'] = Product::whereIn('category_id', $catlist)->groupBy('processing_time')->pluck('processing_time')->toArray();
           $data['supply_ability'] = Product::whereIn('category_id', $catlist)->groupBy('supply_ability')->pluck('supply_ability')->toArray();
           $data['payment_method'] = Product::whereIn('category_id', $catlist)->groupBy('payment_method')->pluck('payment_method')->toArray();
           
           
           
           
           $products = Product::whereIn('category_id', $catlist);
           
           if ($request->filled('supplier_id')) {
                $products = $products->where('supplier_id', $request->supplier_id);
            }
           if ($request->filled('country_id')) {
                $products = $products->where('country_id', $request->country_id);
            }
           if ($request->filled('ptime')) {
                $products = $products->where('processing_time', $request->ptime);
            }
           
           if ($request->filled('port')) {
                $products = $products->where('port', $request->port);
            }
            if ($request->filled('unit')) {
                $products = $products->where('unit', $request->unit);
            }
           $data['products'] = $products->paginate(18);
           
           return view('frontend.pages.list_products')->with($data);
        }
    }
    
}