|
Server : Apache/2.4.41 (Ubuntu) System : Linux vmi1525618.contaboserver.net 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64 User : www-data ( 33) PHP Version : 8.2.12 Disable Function : NONE Directory : /var/www/lecturebazaar.com/app/Models/ |
Upload File : |
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
use Astrotomic\Translatable\Translatable;
class ProductCategory extends Model implements TranslatableContract
{
use Translatable;
protected $table = 'product_categories';
public $timestamps = false;
protected $dateFormat = 'U';
protected $guarded = ['id'];
public $translatedAttributes = ['title'];
public function getTitleAttribute()
{
return getTranslateAttributeValue($this, 'title');
}
public function category()
{
return $this->belongsTo('App\Models\ProductCategory', 'parent_id', 'id');
}
public function subCategories()
{
return $this->hasMany($this, 'parent_id', 'id')->orderBy('order', 'asc');
}
public function filters()
{
return $this->hasMany('App\Models\ProductFilter', 'category_id', 'id');
}
public function products()
{
return $this->hasMany('App\Models\Product', 'category_id', 'id');
}
public function getUrl()
{
return '/products?category_id=' . $this->id;
}
public function getSelfAndChideProductsCount($productType = null)
{
$ids = [$this->id];
$subCategoriesIds = $this->subCategories->pluck('id')->toArray();
$ids = array_merge($ids, $subCategoriesIds);
$query = Product::whereIn('category_id', $ids);
if (!empty($productType)) {
$query->where('type', $productType);
}
return $query->count();
}
}