comparison src/Generator/Model/stubs/model.pivot.stub @ 34:f65ab84ee47f default

merge with codex
author luka
date Wed, 10 Sep 2025 21:00:47 -0400
parents b17f81b804ff
children
comparison
equal deleted inserted replaced
10:a9ff874afdbd 34:f65ab84ee47f
8 { 8 {
9 // 9 //
10 10
11 /** 11 /**
12 * Indicates if the model should be timestamped. 12 * Indicates if the model should be timestamped.
13 * By default our pivots will not use timestamps 13 * By default our pivots will not use timestamps
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 ];
19 22
20 23
24 //relations
25
26 // BelongsTo
27 # {{ belongs_to_relationships }}
28
29 // HasMany
30 # {{ has_many_relationships }}
31
32 // HasManyThrough
33 # {{ has_many_through_relationships }}
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 }
21 } 105 }