HEX
Server: nginx/1.28.1
System: Linux 10-41-63-61 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64
User: www (1001)
PHP: 7.4.33
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/www.shooperm.com/Modules/Product/Entities/Category.php
<?php

namespace Modules\Product\Entities;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\Cache;
use Modules\Affiliate\Entities\AffiliateCategoryCommission;
use Modules\Appearance\Entities\HeaderCategoryPanel;
use Modules\Appearance\Entities\HeaderSliderPanel;
use Modules\FrontendCMS\Entities\HomepageCustomCategory;
use Modules\Marketing\Entities\NewUserZoneCategory;
use Modules\Marketing\Entities\NewUserZoneCouponCategory;
use Modules\Menu\Entities\MegaMenuRightPanel;
use Modules\Menu\Entities\MenuElement;
use Modules\Seller\Entities\SellerProduct;

class Category extends Model
{
    use HasFactory;
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    protected $guarded = ['id'];


    public static function boot()
    {
        parent::boot();

        self::created(function ($model) {
            Cache::forget('MegaMenu');
            Cache::forget('HeaderSection');
        });
        self::updated(function ($model) {
            Cache::forget('MegaMenu');
            Cache::forget('HeaderSection');
        });
        self::deleted(function ($model) {
            Cache::forget('MegaMenu');
            Cache::forget('HeaderSection');
        });

    }

    public function parentCategory(){

        return $this->belongsTo(Category::class, 'parent_id', 'id');
    }

    public function groups(){

        return $this->hasMany(Group::class,'parent_id','id');
    }

    public function categoryImage(){

        return $this->hasOne(CategoryImage::class,'category_id','id')->withDefault();
    }

    public function brands(){

        return $this->belongsToMany(Brand::class);
    }

    public function products(){
        return $this->belongsToMany(Product::class)->withPivot('category_id', 'product_id');
    }

    public function getSellerProductCountAttribute(){
        $count = 0;
        foreach($this->products as $product){
            $count += $product->sellerProducts->count();
        }
        return $count;
    }


    // public function sellerProducts(){

    //     $products = SellerProduct::with('seller','product')->whereHas('product',function($query){
    //         return $query->whereHas('categories',function($q){
    //             $q->where('category_id',$this->id);
    //         })->where('status', 1);
    //     })->activeSeller()->take(6)->get();
    //     // $products = SellerProduct::with('seller','product')->take(6)->get();
    //     // return collect();
    //     return $products;

    // }

    public function sellerProducts()
    {
        return $this->hasManyDeep(SellerProduct::class, 
        [
            CategoryProduct::class,
            Product::class
        ],
        [
            'category_id', // Foreign key on the "pivot" table for category.
            'id',     // Foreign key on the "comments" table.
            'product_id'    // Foreign key on the "seller_product" table.
        ],
        [  
            'id',               // Local key on "category" table
            'product_id',          // Local key on pivot table
            'id'                // Local key on "product" table
        ]
        )->activeSeller();
    }

    public function sellerProductsAll(){
        return SellerProduct::where('status',1)->whereHas('product',function($query){
            return $query->whereHas('categories',function($q){
                $q->where('category_id', $this->id);
            });
        })->activeSeller()->get();

    }

    public function sellerProductTake($amount = 6){
        return SellerProduct::where('status',1)->whereHas('product',function($query){
            return $query->whereHas('categories',function($q){
                $q->where('category_id', $this->id);
            });
        })->activeSeller()->take($amount)->get();
    }

    public function sellerProductWithPaginate(){
        $products = SellerProduct::with('product','reviews')->where('status',1)->whereHas('product',function($query){
            return $query->whereHas('categories',function($q){
                $q->where('category_id', $this->id);
            })->where('status', 1);
        })->activeSeller()->paginate(12);
        $products->appends([
            'item' => 'category',
            'category' => $this->id
        ]);
        return $products;
    }

    public function subCategories(){
        return $this->hasMany(Category::class,'parent_id','id')->with('subCategories');
    }

    public function newUserZoneCategories(){
        return $this->hasMany(NewUserZoneCategory::class,'category_id','id');
    }

    public function newUserZoneCouponCategories(){
        return $this->hasMany(NewUserZoneCouponCategory::class,'category_id','id');
    }

    public function getMenuElementsAttribute(){
        return MenuElement::where('type', 'category')->where('element_id', $this->id)->get();
    }

    public function getSildersAttribute(){
        return HeaderSliderPanel::where('data_type','category')->where('data_id', $this->id)->get();
    }

    public function headerCategoryPanel(){
        return $this->hasMany(HeaderCategoryPanel::class,'category_id', 'id');
    }

    public function MenuRightPanel(){
        return $this->hasMany(MegaMenuRightPanel::class,'category_id', 'id');
    }

    public function homepageCustomCategories(){
        return $this->hasMany(HomepageCustomCategory::class, 'category_id', 'id');
    }

    //for api
    public function getAllProductsAttribute(){
        return SellerProduct::with('product','reviews')->whereHas('product', function($query){
            return $query->whereHas('categories', function($q){
                $q->where('category_id', $this->id);
            })->where('status', 1);
        })->activeSeller()->where('status', 1)->paginate(10);
    }

    public function affiliateCategoryCommission()
    {
        return $this->hasOne(AffiliateCategoryCommission::class,'category_id');
    }

    protected static function categoryFactory()
    {
        return \Modules\Product\Database\factories\CategoryFactory::new();
    }

    protected static function subcategoryFactory(){
        return \Modules\Product\Database\factories\SubcategoryFactory::new();
    }

}