This commit is contained in:
Kevin Adametz 2024-08-05 12:05:24 +02:00
parent 04d677d37a
commit bfa3bb1df4
1191 changed files with 637397 additions and 10619 deletions

View file

@ -45,6 +45,8 @@ use Cviebrock\EloquentSluggable\Sluggable;
* @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
@ -53,7 +55,6 @@ class Category extends Model
protected $table = 'categories';
protected $casts = ['trans_name' => 'array', 'trans_headline' => 'array'];
protected $fillable = [
'parent_id', 'name', 'headline', 'pos', 'active',
@ -84,6 +85,27 @@ class Category extends Model
}
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 iq_image()
{
return $this->belongsTo('App\Models\IqImage', 'headline_image_id');
@ -93,44 +115,29 @@ class Category extends Model
$this->attributes['pos'] = is_numeric($value) ? $value : null;
}
public function getLang($key)
public function translations()
{
return $this->hasMany(TransCategory::class, 'categorie_id');
}
public function getLang($key)
{
$lang = \App::getLocale();
if ($lang === 'de') {
if ($lang == 'de') {
return $this->{$key};
}
$trans = $this->getTrans($key, $lang);
if (!$trans || $trans == '') {
return $this->{$key};
}
return $trans;
return $trans != '' ? $trans : $this->{$key};
}
public function getTrans($key, $lang)
{
$key = 'trans_' . $key;
if (!empty($this->{$key}[$lang])) {
return $this->{$key}[$lang];
}
return "";
}
public function getTranNames()
{
$ret = "";
foreach ((array) $this->trans_name as $value){
$ret .= $value.', ';
}
return rtrim($ret, ', ');
}
public function getTranHeadlines()
{
$ret = "";
foreach ((array) $this->trans_headline as $value){
$ret .= $value.', ';
}
return rtrim($ret, ', ');
$trans = $this->translations->where('language','=', $lang)->where('key', $key)->first();
return $trans ? $trans->value : '';
}
}