. */ namespace Modules\Articoli; use Common\SimpleModelTrait; use Illuminate\Database\Eloquent\Model; use Traits\HierarchyTrait; class Categoria extends Model { use SimpleModelTrait; use HierarchyTrait; protected $table = 'mg_categorie'; protected static $parent_identifier = 'parent'; public static function build($nota, $colore) { $model = new static(); $model->nota = $nota; $model->colore = $colore; $model->save(); return $model; } public function articoli() { return $this->hasMany(Articolo::class, 'id_categoria'); } /** * Ritorna l'attributo name della categoria articolo. * * @return string */ public function getNameAttribute() { return database()->table($this->table.'_lang') ->select('name') ->where('id_record', '=', $this->id) ->where('id_lang', '=', \App::getLang()) ->first()->name; } /** * Ritorna l'id della categoria articolo a partire dal nome. * * @param string $name il nome da ricercare * * @return \Illuminate\Support\Collection */ public function getByName($name) { return database()->table($this->table.'_lang') ->select('id_record') ->where('name', '=', $name) ->where('id_lang', '=', \App::getLang()) ->first(); } /** * Imposta l'attributo name della categoria. */ public function setNameAttribute($value) { $table = database()->table($this->table.'_lang'); $translated = $table ->where('id_record', '=', $this->id) ->where('id_lang', '=', \App::getLang()); if ($translated->count() > 0) { $translated->update([ 'name' => $value ]); } else { $table->insert([ 'id_record' => $this->id, 'id_lang' => \App::getLang(), 'name' => $value ]); } } }