Server : nginx/1.18.0 System : Linux localhost 6.14.3-x86_64-linode168 #1 SMP PREEMPT_DYNAMIC Mon Apr 21 19:47:55 EDT 2025 x86_64 User : www-data ( 33) PHP Version : 8.0.16 Disable Function : 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_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /var/www/ecommerce/app/Models/ |
<?php
namespace App\Models;
use App\Traits\ActiveStatusTrait;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use ActiveStatusTrait;
protected $fillable = [
'title',
'slug',
'summary',
'photo',
'status',
'is_parent',
'parent_id',
'added_by',
'payment_method_id',
'sort_order',
];
public function parent_info()
{
return $this->hasOne(Category::class, 'id', 'parent_id');
}
public static function getAllCategory()
{
return static::orderBy('sort_order', 'ASC')->with('parent_info')->paginate(10);
}
public static function shiftChild($cat_id)
{
return Category::whereIn('id', $cat_id)->update(['is_parent' => 1]);
}
public static function getChildByParentID($id)
{
return Category::where('parent_id', $id)->orderBy('id', 'ASC')->pluck('title', 'id');
}
public function child_cat()
{
return $this->hasMany(Category::class, 'parent_id', 'id')->where('status', 'active');
}
public static function getAllParentWithChild()
{
return Category::with('child_cat')->where('is_parent', 1)->where('status', 'active')->orderBy('sort_order', 'ASC')->toBase()->get();
}
public function products()
{
return $this->hasMany(Product::class, 'cat_id', 'id')->where('status', 'active');
}
public function sub_products()
{
return $this->hasMany(Product::class, 'child_cat_id', 'id')->where('status', 'active');
}
public static function getProductByCat($slug)
{
return Category::with('products')->where('slug', $slug)->first();
// return Product::where('cat_id',$id)->where('child_cat_id',null)->paginate(10);
}
public static function getProductBySubCat($slug)
{
// return $slug;
return Category::with('sub_products')->where('slug', $slug)->first();
}
public static function countActiveCategory()
{
$data = Category::where('status', 'active')->count();
if ($data) {
return $data;
}
return 0;
}
}