X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;ds=sidebyside;f=app%2FModels%2FTechnique.php;h=0b25e47b19ded74f56c91a8c65a662c1021e2eae;hb=e4e7cbc7e4944b08d74f5752de337ba7700367f4;hp=202c455945736ae2107ff21bbd4ce594ade2ccaa;hpb=68aabaf6da8ed6e675bdea728702d5bd75066964;p=alttp.git diff --git a/app/Models/Technique.php b/app/Models/Technique.php index 202c455..0b25e47 100644 --- a/app/Models/Technique.php +++ b/app/Models/Technique.php @@ -4,8 +4,50 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\App; class Technique extends Model { use HasFactory; + + public function chapters() { + return $this + ->belongsToMany(Technique::class, 'technique_chapter', 'parent_id', 'child_id') + ->withPivot('level', 'order') + ->orderByPivot('order') + ->using(TechniqueChapter::class); + } + + public function relations() { + return $this + ->belongsToMany(Technique::class, 'technique_relations', 'from_id', 'to_id') + ->withPivot('type') + ->using(TechniqueRelation::class); + } + + public function translations() { + return $this->hasMany(TechniqueTranslation::class); + } + + public function getTranslatedProperty($prop, $lang = null) { + if (is_null($lang)) $lang = App::getLocale(); + foreach ($this->translations as $translation) { + if ($translation->locale == $lang) { + return $translation->{$prop}; + } + } + return $this->{$prop}; + } + + protected $casts = [ + 'index' => 'boolean', + 'requirements' => 'array', + 'rulesets' => 'array', + 'title_icons' => 'array', + ]; + + protected $with = [ + 'translations', + ]; + }