You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
xshop/app/Models/Product.php

189 lines
5.2 KiB
PHTML

6 months ago
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Plank\Metable\Metable;
use Spatie\Image\Enums\AlignPosition;
use Spatie\Image\Enums\Fit;
use Spatie\Image\Enums\Unit;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\Tags\HasTags;
use Spatie\Translatable\HasTranslations;
6 months ago
class Product extends Model implements HasMedia
6 months ago
{
use HasFactory, SoftDeletes, InteractsWithMedia, HasTranslations, HasTags, Metable;
protected $casts = [
'qz' => 'array',
'qidz' => 'array'
];
public function attachs(){
return $this->morphMany(Attachment::class,'attachable');
}
protected $guarded = [];
public function getQzAttribute(){
$result = [];
foreach ($this->quantities as $q) {
if ($q->count > 0){
$q->data = json_decode($q->data);
$result[] = $q;
}
}
return $result;
}
public function getQidzAttribute(){
return $this->quantities()->pluck('id')->toArray();
}
public static $stock_status = ['IN_STOCK', 'OUT_STOCK', 'BACK_ORDER'];
public $translatable = ['name', 'excerpt', 'description','table'];
public function registerMediaConversions(?Media $media = null): void
{
$ti = imageSizeConvertValidate('product_image');
$t = imageSizeConvertValidate('product_thumb');
$mc = $this->addMediaConversion('product-thumb')
->width($t[0])
->height($t[1])
->crop($t[0], $t[1])
->optimize()
->sharpen(10)
->nonQueued()
->format(getSetting('optimize'));
$mc2 = $this->addMediaConversion('product-image')
->width($ti[0])
->height($ti[1])
->crop($ti[0], $ti[1])
->optimize()
->sharpen(10)
->nonQueued()
->format(getSetting('optimize'));
if (getSetting('watermark')){
$mc->watermark(public_path('upload/images/logo.png'),
AlignPosition::BottomLeft,5,5,Unit::Percent,
15,Unit::Percent,15,Unit::Percent,Fit::Contain,50);
$mc2->watermark(public_path('upload/images/logo.png'),
AlignPosition::BottomLeft,5,5,Unit::Percent,
15,Unit::Percent,15,Unit::Percent,Fit::Contain,50);
}
}
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
public function approvedComments()
{
return $this->morphMany(Comment::class, 'commentable')->where('status', 1)->whereNull('sub_comment_id');
}
public function categories()
{
return $this->belongsToMany(Category::class);
}
public function category()
{
return $this->belongsTo(Category::class, 'category_id', 'id');
}
public function getRouteKeyName()
{
return 'slug';
}
public function quantities()
{
return $this->hasMany(Quantity::class);
}
public function discounts()
{
return $this->hasMany(Discount::class, 'product_id', 'id');
}
public function activeDiscounts()
{
return $this->hasMany(Discount::class, 'product_id', 'id')->where(function ($query) {
$query->where('expire', '>=', date('Y-m-d'))
->orWhereNull('expire');
});
}
public function quesions()
{
return $this->hasMany(Question::class);
}
function hasDiscount()
{
return $this->discounts()->where('expire', '>', date('Y-m-d'))->count() > 0;
}
public function isFav()
{
if (auth('customer')->check()) {
return \auth('customer')->user()->products()->where('product_id', $this->id)->exists();
} else {
return false;
}
}
public function imgUrl(){
if ($this->getMedia()->count() > 0) {
return $this->getMedia()[$this->image_index]->getUrl('product-image');
} else {
return asset('assets/upload/logo.svg');
}
}
public function orginalImageUrl(){
if ($this->getMedia()->count() > 0) {
return $this->getMedia()[$this->image_index]->getUrl();
} else {
return asset('assets/upload/logo.svg');
}
}
public function imgUrl2(){
if ($this->getMedia()->count() > 0 && isset($this->getMedia()[1])) {
return $this->getMedia()[1]->getUrl('product-image');
} else {
return asset('assets/upload/logo.svg');
}
}
public function thumbUrl(){
if ($this->getMedia()->count() > 0) {
return $this->getMedia()[$this->image_index]->getUrl('product-thumb');
} else {
return asset('assets/upload/logo.svg');
}
}
public function thumbUrl2(){
if ($this->getMedia()->count() > 0 && isset($this->getMedia()[1])) {
return $this->getMedia()[1]->getUrl('product-thumb');
} else {
return asset('assets/upload/logo.svg');
}
}
6 months ago
}