annotate src/Models/BaseModel.php @ 7:e6132a1e8e24 default tip

Adding better support for test among other base changes.
author Luka Sitas <sitas.luka.97@gmail.com>
date Thu, 25 Sep 2025 19:58:32 -0400
parents b44434aaa767
children
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
7
e6132a1e8e24 Adding better support for test among other base changes.
Luka Sitas <sitas.luka.97@gmail.com>
parents: 2
diff changeset
5 use Illuminate\Database\Eloquent\Model;
2
b44434aaa767 Moving around the components.
luka
parents: 1
diff changeset
6 use Wizard\Framework\Database\Builder;
7
e6132a1e8e24 Adding better support for test among other base changes.
Luka Sitas <sitas.luka.97@gmail.com>
parents: 2
diff changeset
7 use Wizard\Framework\Traits\Filterable;
1
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
7
e6132a1e8e24 Adding better support for test among other base changes.
Luka Sitas <sitas.luka.97@gmail.com>
parents: 2
diff changeset
13 protected static string $builder = Builder::class;
2
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) {
7
e6132a1e8e24 Adding better support for test among other base changes.
Luka Sitas <sitas.luka.97@gmail.com>
parents: 2
diff changeset
22 if ($user_id = (\Auth::user()->id ?? null)) {
e6132a1e8e24 Adding better support for test among other base changes.
Luka Sitas <sitas.luka.97@gmail.com>
parents: 2
diff changeset
23 $item->created_by = $user_id;
e6132a1e8e24 Adding better support for test among other base changes.
Luka Sitas <sitas.luka.97@gmail.com>
parents: 2
diff changeset
24 $item->updated_by = $user_id;
e6132a1e8e24 Adding better support for test among other base changes.
Luka Sitas <sitas.luka.97@gmail.com>
parents: 2
diff changeset
25 }
1
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
26 });
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
27
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
28 self::saving(function ($item) {
7
e6132a1e8e24 Adding better support for test among other base changes.
Luka Sitas <sitas.luka.97@gmail.com>
parents: 2
diff changeset
29 if ($user_id = (\Auth::user()->id ?? null)) {
e6132a1e8e24 Adding better support for test among other base changes.
Luka Sitas <sitas.luka.97@gmail.com>
parents: 2
diff changeset
30 $item->updated_by = $user_id;
e6132a1e8e24 Adding better support for test among other base changes.
Luka Sitas <sitas.luka.97@gmail.com>
parents: 2
diff changeset
31 }
1
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
32 });
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
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
35 protected static function load_auxilary_data()
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
36 {
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
37 $data = [];
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
38
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
39 $instance = new static;
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
40
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
41 foreach ($instance->default_relations as $relation) {
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
42 $related_model = $instance->$relation()->getRelated();
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
43 $related_table = $related_model->getTable();
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
44 $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
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 return $data;
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
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
50 public static function load_index()
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 return static::load_auxilary_data();
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
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
55 public static function load_create()
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 return static::load_auxilary_data();
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
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
60 public static function load_edit()
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
61 {
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
62 return static::load_auxilary_data();
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
63 }
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
64
7
e6132a1e8e24 Adding better support for test among other base changes.
Luka Sitas <sitas.luka.97@gmail.com>
parents: 2
diff changeset
65 public static function named_joins()
e6132a1e8e24 Adding better support for test among other base changes.
Luka Sitas <sitas.luka.97@gmail.com>
parents: 2
diff changeset
66 {
e6132a1e8e24 Adding better support for test among other base changes.
Luka Sitas <sitas.luka.97@gmail.com>
parents: 2
diff changeset
67 return [];
e6132a1e8e24 Adding better support for test among other base changes.
Luka Sitas <sitas.luka.97@gmail.com>
parents: 2
diff changeset
68 }
2
b44434aaa767 Moving around the components.
luka
parents: 1
diff changeset
69
1
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 * 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
72 *
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
73 * @return \Illuminate\Database\Eloquent\Builder
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 public static function data_query()
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 $query = static::query();
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
78
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
79 $instance = new static;
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 foreach ($instance->default_relations as $relation) {
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
82 $query->with($relation);
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 return $query;
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
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 * 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
90 *
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
91 * @return \Illuminate\Database\Eloquent\Builder
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 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
94 {
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
95 $query = static::data_query();
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 $query->filter($validated);
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
98
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
99 return $query->get();
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
100 }
56d9c64d64aa Setting up the base, still have plenty of work to be done here.
luka
parents:
diff changeset
101 }