mivita/app/Models/Category.php
2026-01-23 17:35:23 +01:00

139 lines
5.3 KiB
PHP

<?php
namespace App\Models;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
/**
* App\Models\Category
*
* @property int $id
* @property int|null $parent_id
* @property string $name
* @property array|null $trans_name
* @property int|null $pos
* @property int $active
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Category[] $childrens
* @property-read \App\Models\Category|null $parent
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereParentId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category wherePos($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereTransName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereUpdatedAt($value)
* @property string|null $slug
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductCategory[] $product_categories
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category findSimilarSlugs($attribute, $config, $slug)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereSlug($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category query()
* @property string|null $headline
* @property int|null $headline_image_id
* @property array|null $trans_headline
* @property-read int|null $childrens_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Image[] $image
* @property-read int|null $image_count
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereHeadline($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereHeadlineImageId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereTransHeadline($value)
* @property-read \App\Models\IqImage|null $iq_image
* @property-read int|null $product_categories_count
* @method static \Illuminate\Database\Eloquent\Builder|Category withUniqueSlugConstraints(\Illuminate\Database\Eloquent\Model $model, string $attribute, array $config, string $slug)
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\TransCategory> $translations
* @property-read int|null $translations_count
* @mixin \Eloquent
*/
class Category extends Model
{
protected $table = 'categories';
protected $fillable = [
'parent_id',
'name',
'headline',
'pos',
'active',
'slug',
];
public function parent()
{
return $this->belongsTo('App\Models\Category', 'parent_id');
}
public function childrens()
{
return $this->hasMany('App\Models\Category', 'parent_id', 'id');
}
public function product_categories()
{
return $this->hasMany('App\Models\ProductCategory', 'category_id', 'id')->orderBy('pos', 'DESC');
}
public function productCategoriesCountActive()
{
$category_id = $this->id;
return Product::where('active', true)->whereHas('product_categories', function ($query) use ($category_id) {
$query->where('category_id', $category_id); //
})->orderBy('pos', 'ASC')->count();
}
public function productCategoriesCountActiveOn($show_on = ['1'])
{
$category_id = $this->id;
return Product::where('active', true)->whereJsonContains('show_on', $show_on)
->whereHas('product_categories', function ($query) use ($category_id) {
$query->where('category_id', $category_id); //
})->orderBy('pos', 'ASC')->count();
}
public function setSlugAttribute($value)
{
$slug = Str::slug($value);
if (self::where('slug', $slug)->exists()) {
$slug = $slug . '-' . self::where('slug', $slug)->count();
}
$this->attributes['slug'] = $slug;
}
public function iq_image()
{
return $this->belongsTo('App\Models\IqImage', 'headline_image_id');
}
public function setPosAttribute($value)
{
$this->attributes['pos'] = is_numeric($value) ? $value : null;
}
public function translations()
{
return $this->hasMany(TransCategory::class, 'categorie_id');
}
public function getLang($key)
{
$lang = \App::getLocale();
if ($lang == 'de') {
return $this->{$key};
}
$trans = $this->getTrans($key, $lang);
return $trans != '' ? $trans : $this->{$key};
}
public function getTrans($key, $lang)
{
$trans = $this->translations->where('language', '=', $lang)->where('key', $key)->first();
return $trans ? $trans->value : '';
}
}