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 Product extends Model
{
use ActiveStatusTrait;
public const RECENTLY_VIEWED = 'etiqa_recently_viewed';
protected $fillable = [
'title',
'slug',
'summary',
'description',
'cat_id',
'child_cat_id',
'price',
'brand_id',
'discount',
'status',
'photo',
'size',
'stock',
'notify_min_stock',
'is_featured',
'condition',
'payment_method_id',
];
protected $casts = [
'created_at' => 'datetime',
];
public function cat_info()
{
return $this->hasOne(Category::class, 'id', 'cat_id');
}
public function sub_cat_info()
{
return $this->hasOne(Category::class, 'id', 'child_cat_id');
}
public function brand()
{
return $this->belongsTo(Brand::class);
}
public static function getAllProduct()
{
return Product::with(['cat_info', 'sub_cat_info', 'brand'])->orderBy('id', 'desc')->paginate(10);
}
public function relatedProducts()
{
return $this->hasMany(Product::class, 'cat_id', 'cat_id')->where('status', 'active')->orderBy('id', 'DESC')->limit(8);
}
public function getReview()
{
return $this->hasMany(ProductReview::class, 'product_id', 'id')->with('user_info')->where('status', 'active')->orderBy('id', 'DESC');
}
public function reviews()
{
return $this->hasMany(ProductReview::class, 'product_id', 'id');
}
public static function getProductBySlug($slug)
{
return Product::with(['cat_info', 'relatedProducts', 'getReview'])->where('slug', $slug)->first();
}
public static function countActiveProduct()
{
$data = Product::where('status', 'active')->count();
if ($data) {
return $data;
}
return 0;
}
public function carts()
{
return $this->hasMany(Cart::class)->whereNotNull('order_id');
}
public function wishlists()
{
return $this->hasMany(Wishlist::class); //->whereNotNull('cart_id');
}
public function getDiscountedPriceAttribute()
{
return $this->price - ($this->price * $this->discount) / 100;
}
public static function getProductsByCategorySlug(string $slug)
{
$categoryId = Category::whereSlug($slug)->value('id');
return Product::with(['cat_info', 'relatedProducts', 'getReview'])->where('cat_id', $categoryId)->paginate(10);
}
public static function getProductsBySubCategorySlug(string $slug)
{
$subCategoryId = Category::whereSlug($slug)->value('id');
return Product::with(['cat_info', 'relatedProducts', 'getReview'])->where('child_cat_id', $subCategoryId)->paginate(10);
}
public static function getByBrand($slug)
{
$brandId = Brand::whereSlug($slug)->value('id');
return Product::with(['cat_info', 'relatedProducts', 'getReview'])->where('brand_id', $brandId)->paginate(10);
}
}