osb/app/Models/Product.php
2018-08-10 00:10:51 +10:00

75 lines
1.4 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
class Product extends Model
{
protected $table = 'ab_product';
protected $with = ['descriptions'];
public function descriptions()
{
return $this->hasMany(ProductTranslate::class);
}
public function services()
{
return $this->hasMany(Service::class);
}
public function getCategoryAttribute()
{
return $this->prod_plugin_file;
}
public function getDescriptionAttribute()
{
// @todo If the user has selected a specific language.
return $this->description($this->getDefaultLanguage());
}
public function getNameAttribute()
{
return $this->name(Auth::user()->language);
}
public function getProductIdAttribute()
{
return sprintf('#%04s',$this->id);
}
public function scopeActive()
{
return $this->where('active',TRUE);
}
public function description(Language $lo=NULL)
{
if (is_null($lo))
$lo = $this->getDefaultLanguage();
return $this->descriptions->where('language_id',$lo->id)->first()->description_short;
}
private function getDefaultLanguage()
{
return config('SITE_SETUP')->language;
}
/**
* Get the product name
*
* @param Language $lo
* @return string Product Name
*/
public function name(Language $lo=NULL)
{
if (is_null($lo))
$lo = $this->getDefaultLanguage();
return $this->descriptions->where('language_id',$lo->id)->first()->name;
}
}