]> git.localhorst.tv Git - alttp.git/blob - app/Models/Technique.php
serverside tech init
[alttp.git] / app / Models / Technique.php
1 <?php
2
3 namespace App\Models;
4
5 use Illuminate\Database\Eloquent\Factories\HasFactory;
6 use Illuminate\Database\Eloquent\Model;
7 use Illuminate\Support\Facades\App;
8
9 class Technique extends Model
10 {
11         use HasFactory;
12
13         public function chapters() {
14                 return $this
15                         ->belongsToMany(Technique::class, 'technique_chapter', 'parent_id', 'child_id')
16                         ->withPivot('level', 'order')
17                         ->orderByPivot('order')
18                         ->using(TechniqueChapter::class);
19         }
20
21         public function relations() {
22                 return $this
23                         ->belongsToMany(Technique::class, 'technique_relations', 'from_id', 'to_id')
24                         ->withPivot('type')
25                         ->using(TechniqueRelation::class);
26         }
27
28         public function translations() {
29                 return $this->hasMany(TechniqueTranslation::class);
30         }
31
32         public function getTranslatedProperty($prop, $lang = null) {
33                 if (is_null($lang)) $lang = App::getLocale();
34                 foreach ($this->translations as $translation) {
35                         if ($translation->locale == $lang) {
36                                 return $translation->{$prop};
37                         }
38                 }
39                 return $this->{$prop};
40         }
41
42         protected $casts = [
43                 'index' => 'boolean',
44                 'requirements' => 'array',
45                 'rulesets' => 'array',
46                 'title_icons' => 'array',
47         ];
48
49         protected $with = [
50                 'translations',
51         ];
52
53 }