annotate src/Models/BaseModel.php @ 2:b44434aaa767

Moving around the components. Made a big step in the right direction with the Builder and named joins being accessible.
author luka
date Wed, 18 Jun 2025 22:28:47 -0400
parents 56d9c64d64aa
children e6132a1e8e24
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
1 <?php
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
2
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
3 namespace Wizard\Framework\Models;
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
4
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
5 use Wizard\Framework\Traits\Filterable;
2
b44434aaa767 Moving around the components.
luka
parents: 1
diff changeset
6 use Wizard\Framework\Database\Builder;
1
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
7 use Illuminate\Database\Eloquent\Model;
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
8
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
9 class BaseModel extends Model
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
10 {
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
11 use Filterable;
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
12
2
b44434aaa767 Moving around the components.
luka
parents: 1
diff changeset
13 protected static string $builder = Builder::class;
b44434aaa767 Moving around the components.
luka
parents: 1
diff changeset
14
1
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
15 protected $default_relations = [];
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
16
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
17 public static function boot(): void
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
18 {
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
19 parent::boot();
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
20
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
21 self::creating(function ($item) {
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
22 $item->created_by = \Auth::user()?->id ?? '';
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
23 $item->updated_by = \Auth::user()?->id ?? '';
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
24 });
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
25
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
26 self::saving(function ($item) {
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
27 $item->updated_by = \Auth::user()?->id ?? '';
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
28 });
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
29 }
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
30
2
b44434aaa767 Moving around the components.
luka
parents: 1
diff changeset
31
1
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
32 protected static function load_auxilary_data()
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
33 {
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
34 $data = [];
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
35
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
36 $instance = new static;
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
37
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
38 foreach ($instance->default_relations as $relation) {
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
39 $related_model = $instance->$relation()->getRelated();
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
40 $related_table = $related_model->getTable();
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
41 $data[$related_table] = $related_model->all()->pluck('name', 'id')->toArray();
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
42 }
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
43
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
44 return $data;
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
45 }
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
46
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
47 public static function load_index()
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
48 {
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
49 return static::load_auxilary_data();
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
50 }
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
51
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
52 public static function load_create()
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
53 {
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
54 return static::load_auxilary_data();
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
55 }
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
56
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
57 public static function load_edit()
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
58 {
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
59 return static::load_auxilary_data();
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
60 }
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
61
2
b44434aaa767 Moving around the components.
luka
parents: 1
diff changeset
62 public static function named_joins() {
b44434aaa767 Moving around the components.
luka
parents: 1
diff changeset
63 return [];
b44434aaa767 Moving around the components.
luka
parents: 1
diff changeset
64 }
b44434aaa767 Moving around the components.
luka
parents: 1
diff changeset
65
1
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
66 /**
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
67 * Retrieve a query builder instance with default relations loaded.
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
68 *
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
69 * @return \Illuminate\Database\Eloquent\Builder
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
70 */
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
71 public static function data_query()
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
72 {
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
73 $query = static::query();
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
74
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
75 $instance = new static;
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
76
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
77 foreach ($instance->default_relations as $relation) {
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
78 $query->with($relation);
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
79 }
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
80
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
81 return $query;
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
82 }
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
83
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
84 /**
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
85 * Retrieve a query builder instance with default relations loaded.
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
86 *
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
87 * @return \Illuminate\Database\Eloquent\Builder
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
88 */
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
89 public static function get_data(array $validated = [])
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
90 {
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
91 $query = static::data_query();
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
92
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
93 $query->filter($validated);
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
94
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
95 return $query->get();
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
96 }
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
97 }