Current File : /home/tradevaly/public_html/app/Http/Controllers/OfferController.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\FeaturedCategory;
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 OfferController extends Controller
{
    public function buy_offer(Request $request, $slug)
    {
        $data['countries'] = Country::take(10)->get();
        $data['offer'] = BuyOffer::where('slug', $slug)->firstorFail();
        $tags = json_decode($data['offer']->tags, true);
        if(!empty($tags) && is_array($tags)){
        $data['buyoffer'] = BuyOffer::whereJsonContains("tags", $tags[0])->where(function($query) use($tags){
         foreach ($tags as $tag) {
        if ($tag !== $tags[0]) {
            $query->orWhereJsonContains("tags", $tag);
        }
        }
        })->take(10)->get();
            } else {
         $data['buyoffer'] = BuyOffer::where('category_id', $data['offer']->category_id)->take(10)->get();       
            }
        return view('frontend.pages.buyoffer')->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.buyoffer')->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);
        }
    }
    
}