File: D:/HostingSpaces/ZelfVerkopen/zelfverkopen.nl/app/KommaApp/Products/Models/Product.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\KommaApp\Products\Models;
use App\KommaApp\Kms\Core\NestedSets\Nodes\TranslatableEloquentNode;
use App\KommaApp\Languages\Models\Language;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* Class Product
*
* @package App\KommaApp\Products\Models
* @property int id
* @property int site_id
* @property int active
* @property string code_name
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Languages\Models\Language[] $languages
* @property-read \App\KommaApp\Products\Models\ProductTranslation $translation
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Products\Models\ProductTranslation[] $translations
* @mixin \Eloquent
*/
class Product extends TranslatableEloquentNode
{
protected $table = 'products';
protected $class = Product::class;
/*
* Transient properties on Eloquent models
* These are not saved to database.
*/
public $thumbnail = false;
// public $parent_id;
protected $fillable = ['active', 'code_name', 'price', 'lft', 'rgt', 'tree'];
/**
* Gets the translation models for this model
*
* @return HasMany that resolves to AbstractTranslationModel instances
*/
public function translations(): HasMany
{
return $this->hasMany(ProductTranslation::class);
}
public function languages(): BelongsToMany
{
return $this->belongsToMany(Language::class, 'product_translations')
->withPivot('slug', 'name', 'description')
->withTimestamps();
}
public function getPrice(): string
{
$pricing = $this->price / 100;
if(!is_float($pricing)) return '€ ' . $this->price / 100 .',-';
// Splice on decimal divider
$explodePrice = explode('.', (string) $pricing);
$decimal = (string) $explodePrice[1];
// suffix decimal with zero if length is 1 (so a tenth)
if(strlen($decimal) == 1) $decimal = $decimal . '0';
return '€ ' . $explodePrice[0] . ',' . $decimal;
}
public function __get($key)
{
if($key == "title") {
if($this->translation) {
return $this->translation->name;
}
}
if($key == "parent_id") {
if($this::find($this->id))
return $this->getParentId();
}
return parent::__get($key);
}
}