Mercurial > packages > magicforger
annotate src/Generator/Model/stubs/model.stub @ 26:555bfaa500ac codex
Big update for view creation based on the column type.
| author | Luka Sitas <sitas.luka.97@gmail.com> |
|---|---|
| date | Thu, 15 May 2025 21:50:38 -0400 |
| parents | 31109c61ce02 |
| children | f88d2d5dee30 |
| rev | line source |
|---|---|
| 4 | 1 <?php |
| 2 | |
| 3 namespace {{ namespace }}; | |
| 4 | |
| 5 use Illuminate\Database\Eloquent\Factories\HasFactory; | |
| 6 use Illuminate\Database\Eloquent\Model; | |
| 5 | 7 use Illuminate\Database\Eloquent\SoftDeletes; |
| 4 | 8 |
| 9 class {{ class }} extends Model | |
| 10 { | |
| 5 | 11 //use HasFactory; |
| 12 use SoftDeletes; | |
| 13 | |
| 14 /** | |
| 15 * The table associated with the model. | |
| 16 * | |
| 17 * @var string | |
| 18 */ | |
| 19 protected $table = '{{ tableName }}'; | |
| 20 | |
| 21 /** | |
| 22 * The model's default values for attributes. | |
| 23 * | |
| 24 * @var array | |
| 25 */ | |
| 26 protected $attributes = [ | |
| 27 # {{ atributeInsertPoint }} | |
| 28 ]; | |
| 29 | |
|
26
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
30 protected $fillable = [ |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
11
diff
changeset
|
31 # {{ fillableInsertPoint }} |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
11
diff
changeset
|
32 ]; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
11
diff
changeset
|
33 |
|
26
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
34 protected $default_relations = [ |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
35 # {{ defaultRelationsInsertPoint }} |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
36 ]; |
| 5 | 37 |
|
26
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
38 //MARK FOR MODEL |
| 5 | 39 public static function boot() : void { |
| 40 parent::boot(); | |
| 41 | |
| 42 self::creating(function ($item) { | |
|
26
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
43 $item->created_by = \Auth::user()?->id ?? ''; |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
44 $item->updated_by = \Auth::user()?->id ?? ''; |
| 5 | 45 }); |
| 46 | |
| 47 self::saving(function ($item) { | |
|
26
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
48 $item->updated_by = \Auth::user()?->id ?? ''; |
| 5 | 49 }); |
| 50 } | |
| 11 | 51 |
| 52 | |
| 53 //relations | |
| 54 | |
| 55 // BelongsTo | |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
11
diff
changeset
|
56 # {{ belongs_to_relationships }} |
| 11 | 57 |
| 58 // HasMany | |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
59 # {{ has_many_relationships }} |
| 11 | 60 |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
11
diff
changeset
|
61 // HasManyThrough |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
11
diff
changeset
|
62 # {{ has_many_through_relationships }} |
| 11 | 63 |
|
26
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
64 |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
65 /** |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
66 * Load the default relations for the model. |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
67 * |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
68 * @return $this |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
69 */ |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
70 public function load_relations() { |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
71 foreach($this->default_relations as $relation) { |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
72 $this->load($relation); |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
73 } |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
74 return $this; |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
75 } |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
76 |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
77 //MARK FOR MODEL |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
78 protected static function load_auxilary_data() { |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
79 $data = []; |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
80 |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
81 $instance = new static(); |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
82 |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
83 foreach($instance->default_relations as $relation) { |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
84 $related_model = $instance->$relation()->getRelated(); |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
85 $related_table = $related_model->getTable(); |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
86 $data[$related_table] = $related_model->all()->pluck('name','id')->toArray(); |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
87 } |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
88 |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
89 return $data; |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
90 } |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
91 |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
92 //MARK FOR MODEL |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
93 public static function load_index() { |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
94 return static::load_auxilary_data(); |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
95 } |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
96 |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
97 //MARK FOR MODEL |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
98 public static function load_create() { |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
99 return static::load_auxilary_data(); |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
100 } |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
101 |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
102 //MARK FOR MODEL |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
103 public static function load_edit() { |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
104 return static::load_auxilary_data(); |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
105 } |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
106 |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
107 |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
108 /** |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
109 * Retrieve a query builder instance with default relations loaded. |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
110 * |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
111 * @return \Illuminate\Database\Eloquent\Builder |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
112 */ |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
113 //MARK FOR MODEL |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
114 public static function data_query() { |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
115 $query = static::query(); |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
116 |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
117 $instance = new static(); |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
118 |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
119 foreach($instance->default_relations as $relation) { |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
120 $query->with($relation); |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
121 } |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
122 |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
123 return $query; |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
124 } |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
125 |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
126 /** |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
127 * Retrieve a query builder instance with default relations loaded. |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
128 * |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
129 * @return \Illuminate\Database\Eloquent\Builder |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
130 */ |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
131 public static function get_data() |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
132 { |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
133 return static::data_query()->get(); |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
134 } |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
135 |
| 4 | 136 } |
