comparison src/Generator/Model/stubs/model.pivot.stub @ 27:b17f81b804ff codex

Added support for routes
author Luka Sitas <sitas.luka.97@gmail.com>
date Mon, 02 Jun 2025 21:51:09 -0400
parents 31109c61ce02
children
comparison
equal deleted inserted replaced
26:555bfaa500ac 27:b17f81b804ff
14 * 14 *
15 * @var bool 15 * @var bool
16 */ 16 */
17 public $timestamps = false; 17 public $timestamps = false;
18 18
19 protected $default_relations = [
20 # {{ defaultRelationsInsertPoint }}
21 ];
22
19 23
20 //relations 24 //relations
21 25
22 // BelongsTo 26 // BelongsTo
23 # {{ belongs_to_relationships }} 27 # {{ belongs_to_relationships }}
26 # {{ has_many_relationships }} 30 # {{ has_many_relationships }}
27 31
28 // HasManyThrough 32 // HasManyThrough
29 # {{ has_many_through_relationships }} 33 # {{ has_many_through_relationships }}
30 34
35 /**
36 * Load the default relations for the model.
37 *
38 * @return $this
39 */
40 public function load_relations() {
41 foreach($this->default_relations as $relation) {
42 $this->load($relation);
43 }
44 return $this;
45 }
46
47 //MARK FOR MODEL
48 protected static function load_auxilary_data() {
49 $data = [];
50
51 $instance = new static();
52
53 foreach($instance->default_relations as $relation) {
54 $related_model = $instance->$relation()->getRelated();
55 $related_table = $related_model->getTable();
56 $data[$related_table] = $related_model->all()->pluck('name','id')->toArray();
57 }
58
59 return $data;
60 }
61
62 //MARK FOR MODEL
63 public static function load_index() {
64 return static::load_auxilary_data();
65 }
66
67 //MARK FOR MODEL
68 public static function load_create() {
69 return static::load_auxilary_data();
70 }
71
72 //MARK FOR MODEL
73 public static function load_edit() {
74 return static::load_auxilary_data();
75 }
76
77
78 /**
79 * Retrieve a query builder instance with default relations loaded.
80 *
81 * @return \Illuminate\Database\Eloquent\Builder
82 */
83 //MARK FOR MODEL
84 public static function data_query() {
85 $query = static::query();
86
87 $instance = new static();
88
89 foreach($instance->default_relations as $relation) {
90 $query->with($relation);
91 }
92
93 return $query;
94 }
95
96 /**
97 * Retrieve a query builder instance with default relations loaded.
98 *
99 * @return \Illuminate\Database\Eloquent\Builder
100 */
101 public static function get_data()
102 {
103 return static::data_query()->get();
104 }
31 } 105 }