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/Cat.php

144 lines
4.7 KiB
PHTML

2 years ago
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Image\Manipulations;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\Translatable\HasTranslations;
2 years ago
/**
* App\Models\Cat
*
* @property int $id
* @property string $name
* @property string $slug
* @property string|null $description
* @property int $sort
* @property string|null $image
* @property int|null $parent_id
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Product[] $active_products
* @property-read int|null $active_products_count
* @property-read \Illuminate\Database\Eloquent\Collection|Cat[] $children
* @property-read int|null $children_count
* @property-read \Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection|Media[] $media
* @property-read int|null $media_count
* @property-read Cat|null $parent
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Product[] $products
* @property-read int|null $products_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Prop[] $props
* @property-read int|null $props_count
* @method static \Database\Factories\CatFactory factory(...$parameters)
* @method static \Illuminate\Database\Eloquent\Builder|Cat newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Cat newQuery()
* @method static \Illuminate\Database\Query\Builder|Cat onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|Cat query()
* @method static \Illuminate\Database\Eloquent\Builder|Cat whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Cat whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Cat whereDescription($value)
* @method static \Illuminate\Database\Eloquent\Builder|Cat whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Cat whereImage($value)
* @method static \Illuminate\Database\Eloquent\Builder|Cat whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|Cat whereParentId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Cat whereSlug($value)
* @method static \Illuminate\Database\Eloquent\Builder|Cat whereSort($value)
* @method static \Illuminate\Database\Eloquent\Builder|Cat whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|Cat withTrashed()
* @method static \Illuminate\Database\Query\Builder|Cat withoutTrashed()
* @property int $is_main
* @method static \Illuminate\Database\Eloquent\Builder|Cat whereIsMain($value)
2 years ago
* @mixin \Eloquent
*/
class Cat extends Model implements HasMedia
{
use HasFactory, SoftDeletes, InteractsWithMedia,HasTranslations;
public $translatable = ['name','description'];
2 years ago
public function registerMediaConversions(Media $media = null): void
{
$size = explode('x', config('app.thumbnail_size'));
2 years ago
$this->addMediaConversion('cat-thumb')
->width($size[0])
->height($size[1])
->crop(Manipulations::CROP_CENTER, $size[0], $size[1])
2 years ago
->optimize()
->sharpen(10);
if (isset($_FILES['image'])) {
$this->addMediaFromRequest('image')->preservingOriginal();
}
}
public function thumbUrl()
{
2 years ago
if ($this->getMedia()->count() > 0) {
return $this->getMedia()->first()->getUrl('cat-thumb');
} else {
return asset('/images/logo.png');
}
}
public function imgurl()
{
2 years ago
if ($this->getMedia()->count() > 0) {
return $this->getMedia()->last()->getUrl();
} else {
return asset('/images/logo.png');
}
}
public function products()
{
return $this->belongsToMany(Product::class);
2 years ago
}
public function active_products()
{
return $this->belongsToMany(Product::class)->where('active', 1);
2 years ago
}
public function backUrl()
{
if ($this->image == null) {
return null;
}
return \Storage::url('cats/' . $this->image);
}
//
public function parent()
{
2 years ago
return $this->belongsTo(Cat::class, 'parent_id');
}
public function children()
{
2 years ago
return $this->hasMany(Cat::class, 'parent_id');
}
public function getRouteKeyName()
{
2 years ago
return 'slug';
}
public function props()
{
2 years ago
return $this->belongsToMany(Prop::class);
}
}