comparison 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
comparison
equal deleted inserted replaced
25:1a717c7b211f 26:555bfaa500ac
25 */ 25 */
26 protected $attributes = [ 26 protected $attributes = [
27 # {{ atributeInsertPoint }} 27 # {{ atributeInsertPoint }}
28 ]; 28 ];
29 29
30 protected $fillable =[ 30 protected $fillable = [
31 # {{ fillableInsertPoint }} 31 # {{ fillableInsertPoint }}
32 ]; 32 ];
33 33
34 protected $default_relations = [
35 # {{ defaultRelationsInsertPoint }}
36 ];
34 37
38 //MARK FOR MODEL
35 public static function boot() : void { 39 public static function boot() : void {
36 parent::boot(); 40 parent::boot();
37 41
38 self::creating(function ($item) { 42 self::creating(function ($item) {
39 $item->created_by = \Auth::user()->id; 43 $item->created_by = \Auth::user()?->id ?? '';
40 $item->updated_by = \Auth::user()->id; 44 $item->updated_by = \Auth::user()?->id ?? '';
41 }); 45 });
42 46
43 self::saving(function ($item) { 47 self::saving(function ($item) {
44 $item->updated_by = \Auth::user()->id; 48 $item->updated_by = \Auth::user()?->id ?? '';
45 }); 49 });
46 } 50 }
47 51
48 52
49 //relations 53 //relations
55 # {{ has_many_relationships }} 59 # {{ has_many_relationships }}
56 60
57 // HasManyThrough 61 // HasManyThrough
58 # {{ has_many_through_relationships }} 62 # {{ has_many_through_relationships }}
59 63
64
65 /**
66 * Load the default relations for the model.
67 *
68 * @return $this
69 */
70 public function load_relations() {
71 foreach($this->default_relations as $relation) {
72 $this->load($relation);
73 }
74 return $this;
75 }
76
77 //MARK FOR MODEL
78 protected static function load_auxilary_data() {
79 $data = [];
80
81 $instance = new static();
82
83 foreach($instance->default_relations as $relation) {
84 $related_model = $instance->$relation()->getRelated();
85 $related_table = $related_model->getTable();
86 $data[$related_table] = $related_model->all()->pluck('name','id')->toArray();
87 }
88
89 return $data;
90 }
91
92 //MARK FOR MODEL
93 public static function load_index() {
94 return static::load_auxilary_data();
95 }
96
97 //MARK FOR MODEL
98 public static function load_create() {
99 return static::load_auxilary_data();
100 }
101
102 //MARK FOR MODEL
103 public static function load_edit() {
104 return static::load_auxilary_data();
105 }
106
107
108 /**
109 * Retrieve a query builder instance with default relations loaded.
110 *
111 * @return \Illuminate\Database\Eloquent\Builder
112 */
113 //MARK FOR MODEL
114 public static function data_query() {
115 $query = static::query();
116
117 $instance = new static();
118
119 foreach($instance->default_relations as $relation) {
120 $query->with($relation);
121 }
122
123 return $query;
124 }
125
126 /**
127 * Retrieve a query builder instance with default relations loaded.
128 *
129 * @return \Illuminate\Database\Eloquent\Builder
130 */
131 public static function get_data()
132 {
133 return static::data_query()->get();
134 }
135
60 } 136 }