@@ -814,7 +814,7 @@ discard block |
||
814 | 814 | $columns = $this->columnize(array_keys(headItem($values))); |
815 | 815 | |
816 | 816 | $parameters = collect($values)->map(fn ($record) => '('.$this->parameterize($record).')') |
817 | - ->implode(', '); |
|
817 | + ->implode(', '); |
|
818 | 818 | |
819 | 819 | return "insert into $table ($columns) values $parameters"; |
820 | 820 | } |
@@ -917,7 +917,7 @@ discard block |
||
917 | 917 | */ |
918 | 918 | public function compileUpdateWithoutJoins(Builder $builder, $table, $columns, $where): string |
919 | 919 | { |
920 | - return "update {$table} set {$columns} {$where}"; |
|
920 | + return "update {$table} set {$columns} {$where}"; |
|
921 | 921 | } |
922 | 922 | |
923 | 923 | /** |
@@ -970,7 +970,7 @@ discard block |
||
970 | 970 | */ |
971 | 971 | public function compileDeleteWithoutJoins(Builder $builder, $table, $where): string |
972 | 972 | { |
973 | - return "delete from {$table} {$where}"; |
|
973 | + return "delete from {$table} {$where}"; |
|
974 | 974 | } |
975 | 975 | |
976 | 976 | /** |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | |
158 | 158 | $clauses = []; |
159 | 159 | |
160 | - foreach ($join->clauses as $clause) { |
|
160 | + foreach ($join->clauses as $clause) { |
|
161 | 161 | $clauses[] = $this->compileJoinContraint($clause); |
162 | 162 | } |
163 | 163 | |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | */ |
220 | 220 | protected function compileWheresToArray($query): array |
221 | 221 | { |
222 | - return collect($query->wheres)->map(function ($where) use ($query) { |
|
222 | + return collect($query->wheres)->map(function($where) use ($query) { |
|
223 | 223 | return $where['boolean'].' '.$this->{"where{$where['type']}"}($query, $where); |
224 | 224 | })->all(); |
225 | 225 | } |
@@ -340,7 +340,9 @@ discard block |
||
340 | 340 | */ |
341 | 341 | protected function whereIn(Builder $builder, $where): string |
342 | 342 | { |
343 | - if (empty($where['values'])) return '0 = 1'; |
|
343 | + if (empty($where['values'])) { |
|
344 | + return '0 = 1'; |
|
345 | + } |
|
344 | 346 | |
345 | 347 | return $this->wrap($where['column']).' in ('.$this->parameterize($where['values']).')'; |
346 | 348 | } |
@@ -355,7 +357,9 @@ discard block |
||
355 | 357 | */ |
356 | 358 | protected function whereNotIn(Builder $builder, $where): string |
357 | 359 | { |
358 | - if (empty($where['query'])) return '1 = 1'; |
|
360 | + if (empty($where['query'])) { |
|
361 | + return '1 = 1'; |
|
362 | + } |
|
359 | 363 | |
360 | 364 | $values = $this->parameterize($where['query']); |
361 | 365 | |
@@ -372,7 +376,9 @@ discard block |
||
372 | 376 | */ |
373 | 377 | protected function whereNotInRaw(Builder $builder, $where): string |
374 | 378 | { |
375 | - if (empty($where['values'])) return '1 = 1'; |
|
379 | + if (empty($where['values'])) { |
|
380 | + return '1 = 1'; |
|
381 | + } |
|
376 | 382 | |
377 | 383 | return $this->wrap($where['column']).' not in ('.implode(', ', $where['values']).')'; |
378 | 384 | } |
@@ -387,7 +393,9 @@ discard block |
||
387 | 393 | */ |
388 | 394 | protected function whereInRaw(Builder $builder, $where): string |
389 | 395 | { |
390 | - if (empty($where['values'])) return '0 = 1'; |
|
396 | + if (empty($where['values'])) { |
|
397 | + return '0 = 1'; |
|
398 | + } |
|
391 | 399 | |
392 | 400 | return $this->wrap($where['column']).' in ('.implode(', ', $where['values']).')'; |
393 | 401 | } |
@@ -181,13 +181,13 @@ |
||
181 | 181 | return 'select * from ('.$sql.') as '.$this->wrapTable('temp_table'); |
182 | 182 | } |
183 | 183 | |
184 | - /** |
|
185 | - * Compile a truncate table statement into SQL. |
|
186 | - * |
|
187 | - * @param \Syscodes\Components\Database\Query\Builder $builder |
|
188 | - * |
|
189 | - * @return array |
|
190 | - */ |
|
184 | + /** |
|
185 | + * Compile a truncate table statement into SQL. |
|
186 | + * |
|
187 | + * @param \Syscodes\Components\Database\Query\Builder $builder |
|
188 | + * |
|
189 | + * @return array |
|
190 | + */ |
|
191 | 191 | public function truncate(Builder $builder): array |
192 | 192 | { |
193 | 193 | return ['truncate table '.$this->wrapTable($builder->from) => []]; |
@@ -65,13 +65,13 @@ |
||
65 | 65 | return $connection; |
66 | 66 | } |
67 | 67 | |
68 | - /** |
|
69 | - * Create a DSN string from a configuration. |
|
70 | - * |
|
71 | - * @param array $config |
|
72 | - * |
|
73 | - * @return string |
|
74 | - */ |
|
68 | + /** |
|
69 | + * Create a DSN string from a configuration. |
|
70 | + * |
|
71 | + * @param array $config |
|
72 | + * |
|
73 | + * @return string |
|
74 | + */ |
|
75 | 75 | protected function getDsn(array $config): string |
76 | 76 | { |
77 | 77 | extract($config, EXTR_SKIP); |
@@ -316,7 +316,7 @@ discard block |
||
316 | 316 | return $instance; |
317 | 317 | } |
318 | 318 | |
319 | - return take($this->newModelInstance(array_merge($attributes, $values)), function ($instance) { |
|
319 | + return take($this->newModelInstance(array_merge($attributes, $values)), function($instance) { |
|
320 | 320 | $instance->save(); |
321 | 321 | }); |
322 | 322 | } |
@@ -466,7 +466,7 @@ discard block |
||
466 | 466 | */ |
467 | 467 | protected function getRelation($name) |
468 | 468 | { |
469 | - $relation = Relation::noConstraints(function () use ($name) { |
|
469 | + $relation = Relation::noConstraints(function() use ($name) { |
|
470 | 470 | try { |
471 | 471 | return $this->getModel()->newInstance()->$name(); |
472 | 472 | } catch (BadMethodCallException $e) { |
@@ -62,7 +62,9 @@ |
||
62 | 62 | public function load($relations): static |
63 | 63 | { |
64 | 64 | if (count($this->items) > 0) { |
65 | - if (is_string($relations)) $relations = func_get_args(); |
|
65 | + if (is_string($relations)) { |
|
66 | + $relations = func_get_args(); |
|
67 | + } |
|
66 | 68 | |
67 | 69 | $query = $this->first()->newQuery()->with($relations); |
68 | 70 |
@@ -42,964 +42,964 @@ |
||
42 | 42 | */ |
43 | 43 | class Model implements Arrayable, ArrayAccess |
44 | 44 | { |
45 | - use HasAttributes, |
|
46 | - HasEvents, |
|
47 | - HasRelations, |
|
48 | - HidesAttributes, |
|
49 | - GuardsAttributes, |
|
50 | - ForwardsCalls; |
|
51 | - |
|
52 | - /** |
|
53 | - * The database connection name. |
|
54 | - * |
|
55 | - * @var string|null $connection |
|
56 | - */ |
|
57 | - protected $connection; |
|
58 | - |
|
59 | - /** |
|
60 | - * Indicates if the model exists. |
|
61 | - * |
|
62 | - * @var bool $exists |
|
63 | - */ |
|
64 | - protected $exists = false; |
|
65 | - |
|
66 | - /** |
|
67 | - * Indicates if the IDs are auto-incrementing. |
|
68 | - * |
|
69 | - * @var bool $incrementing |
|
70 | - */ |
|
71 | - protected $incrementing = true; |
|
72 | - |
|
73 | - /** |
|
74 | - * The primary key for the model. |
|
75 | - * |
|
76 | - * @var string $primaryKey |
|
77 | - */ |
|
78 | - protected $primaryKey = 'id'; |
|
79 | - |
|
80 | - /** |
|
81 | - * The table associated with the model. |
|
82 | - * |
|
83 | - * @var string $table |
|
84 | - */ |
|
85 | - protected $table; |
|
86 | - |
|
87 | - /** |
|
88 | - * Indicates if the model should be timestamped. |
|
89 | - * |
|
90 | - * @var bool $timestamps |
|
91 | - */ |
|
92 | - protected $timestamps = true; |
|
93 | - |
|
94 | - /** |
|
95 | - * The array of booted models. |
|
96 | - * |
|
97 | - * @var array $booted |
|
98 | - */ |
|
99 | - protected static $booted = []; |
|
45 | + use HasAttributes, |
|
46 | + HasEvents, |
|
47 | + HasRelations, |
|
48 | + HidesAttributes, |
|
49 | + GuardsAttributes, |
|
50 | + ForwardsCalls; |
|
51 | + |
|
52 | + /** |
|
53 | + * The database connection name. |
|
54 | + * |
|
55 | + * @var string|null $connection |
|
56 | + */ |
|
57 | + protected $connection; |
|
58 | + |
|
59 | + /** |
|
60 | + * Indicates if the model exists. |
|
61 | + * |
|
62 | + * @var bool $exists |
|
63 | + */ |
|
64 | + protected $exists = false; |
|
65 | + |
|
66 | + /** |
|
67 | + * Indicates if the IDs are auto-incrementing. |
|
68 | + * |
|
69 | + * @var bool $incrementing |
|
70 | + */ |
|
71 | + protected $incrementing = true; |
|
72 | + |
|
73 | + /** |
|
74 | + * The primary key for the model. |
|
75 | + * |
|
76 | + * @var string $primaryKey |
|
77 | + */ |
|
78 | + protected $primaryKey = 'id'; |
|
79 | + |
|
80 | + /** |
|
81 | + * The table associated with the model. |
|
82 | + * |
|
83 | + * @var string $table |
|
84 | + */ |
|
85 | + protected $table; |
|
86 | + |
|
87 | + /** |
|
88 | + * Indicates if the model should be timestamped. |
|
89 | + * |
|
90 | + * @var bool $timestamps |
|
91 | + */ |
|
92 | + protected $timestamps = true; |
|
93 | + |
|
94 | + /** |
|
95 | + * The array of booted models. |
|
96 | + * |
|
97 | + * @var array $booted |
|
98 | + */ |
|
99 | + protected static $booted = []; |
|
100 | 100 | |
101 | - /** |
|
102 | - * The connection resolver instance. |
|
103 | - * |
|
104 | - * @var \Syscodes\Components\Database\ConnectionResolverInterface |
|
105 | - */ |
|
106 | - protected static $resolver; |
|
107 | - |
|
108 | - /** |
|
109 | - * The relations to eager load on every query. |
|
110 | - * |
|
111 | - * @var array $with |
|
112 | - */ |
|
113 | - protected $with = []; |
|
114 | - |
|
115 | - /** |
|
116 | - * The name of the "created at" column. |
|
117 | - * |
|
118 | - * @var string|null |
|
119 | - */ |
|
120 | - const CREATED_AT = 'created_at'; |
|
121 | - |
|
122 | - /** |
|
123 | - * The name of the "updated at" column. |
|
124 | - * |
|
125 | - * @var string|null |
|
126 | - */ |
|
127 | - const UPDATED_AT = 'updated_at'; |
|
128 | - |
|
129 | - /** |
|
130 | - * Constructor. The create new Model instance. |
|
131 | - * |
|
132 | - * @param array $attributes |
|
133 | - * |
|
134 | - * @return void |
|
135 | - */ |
|
136 | - public function __construct(array $attributes = []) |
|
137 | - { |
|
138 | - $this-> bootIfNotBooted(); |
|
139 | - |
|
140 | - $this->syncOriginal(); |
|
141 | - |
|
142 | - $this->fill($attributes); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Check if the model needs to be booted and if so, do it. |
|
147 | - * |
|
148 | - * @return void |
|
149 | - */ |
|
150 | - public function bootIfNotBooted(): void |
|
151 | - { |
|
152 | - if ( ! isset(static::$booted[static::class])) { |
|
153 | - static::$booted[static::class] = true; |
|
154 | - |
|
155 | - $this->fireModelEvent('booting', false); |
|
156 | - |
|
157 | - static::boot(); |
|
158 | - |
|
159 | - $this->fireModelEvent('booted', false); |
|
160 | - } |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * The "booting" method of the model. |
|
165 | - * |
|
166 | - * @return void |
|
167 | - */ |
|
168 | - public static function boot(): void |
|
169 | - { |
|
170 | - $class = static::class; |
|
171 | - |
|
172 | - static::$mutatorCache[$class] = []; |
|
173 | - |
|
174 | - foreach (get_class_methods($class) as $method) { |
|
175 | - if (preg_match('/^get(.+)Attribute$/', $method, $matches)) { |
|
176 | - if (static::$snakeAttributes) { |
|
177 | - $matches[1] = Str::snake($matches[1]); |
|
178 | - } |
|
179 | - |
|
180 | - static::$mutatorCache[$class][] = lcfirst($matches[1]); |
|
181 | - } |
|
182 | - } |
|
183 | - |
|
184 | - static::bootTraits(); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Boot all of the bootable traits on the model. |
|
189 | - * |
|
190 | - * @return void |
|
191 | - */ |
|
192 | - protected static function bootTraits() |
|
193 | - { |
|
194 | - $class = static::class; |
|
195 | - |
|
196 | - foreach (class_recursive($class) as $trait) { |
|
197 | - if (method_exists($class, $method = 'boot'.class_basename($trait))) { |
|
198 | - forward_static_call([$class, $method]); |
|
199 | - } |
|
200 | - } |
|
201 | - } |
|
101 | + /** |
|
102 | + * The connection resolver instance. |
|
103 | + * |
|
104 | + * @var \Syscodes\Components\Database\ConnectionResolverInterface |
|
105 | + */ |
|
106 | + protected static $resolver; |
|
107 | + |
|
108 | + /** |
|
109 | + * The relations to eager load on every query. |
|
110 | + * |
|
111 | + * @var array $with |
|
112 | + */ |
|
113 | + protected $with = []; |
|
114 | + |
|
115 | + /** |
|
116 | + * The name of the "created at" column. |
|
117 | + * |
|
118 | + * @var string|null |
|
119 | + */ |
|
120 | + const CREATED_AT = 'created_at'; |
|
121 | + |
|
122 | + /** |
|
123 | + * The name of the "updated at" column. |
|
124 | + * |
|
125 | + * @var string|null |
|
126 | + */ |
|
127 | + const UPDATED_AT = 'updated_at'; |
|
128 | + |
|
129 | + /** |
|
130 | + * Constructor. The create new Model instance. |
|
131 | + * |
|
132 | + * @param array $attributes |
|
133 | + * |
|
134 | + * @return void |
|
135 | + */ |
|
136 | + public function __construct(array $attributes = []) |
|
137 | + { |
|
138 | + $this-> bootIfNotBooted(); |
|
139 | + |
|
140 | + $this->syncOriginal(); |
|
141 | + |
|
142 | + $this->fill($attributes); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Check if the model needs to be booted and if so, do it. |
|
147 | + * |
|
148 | + * @return void |
|
149 | + */ |
|
150 | + public function bootIfNotBooted(): void |
|
151 | + { |
|
152 | + if ( ! isset(static::$booted[static::class])) { |
|
153 | + static::$booted[static::class] = true; |
|
154 | + |
|
155 | + $this->fireModelEvent('booting', false); |
|
156 | + |
|
157 | + static::boot(); |
|
158 | + |
|
159 | + $this->fireModelEvent('booted', false); |
|
160 | + } |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * The "booting" method of the model. |
|
165 | + * |
|
166 | + * @return void |
|
167 | + */ |
|
168 | + public static function boot(): void |
|
169 | + { |
|
170 | + $class = static::class; |
|
171 | + |
|
172 | + static::$mutatorCache[$class] = []; |
|
173 | + |
|
174 | + foreach (get_class_methods($class) as $method) { |
|
175 | + if (preg_match('/^get(.+)Attribute$/', $method, $matches)) { |
|
176 | + if (static::$snakeAttributes) { |
|
177 | + $matches[1] = Str::snake($matches[1]); |
|
178 | + } |
|
179 | + |
|
180 | + static::$mutatorCache[$class][] = lcfirst($matches[1]); |
|
181 | + } |
|
182 | + } |
|
183 | + |
|
184 | + static::bootTraits(); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Boot all of the bootable traits on the model. |
|
189 | + * |
|
190 | + * @return void |
|
191 | + */ |
|
192 | + protected static function bootTraits() |
|
193 | + { |
|
194 | + $class = static::class; |
|
195 | + |
|
196 | + foreach (class_recursive($class) as $trait) { |
|
197 | + if (method_exists($class, $method = 'boot'.class_basename($trait))) { |
|
198 | + forward_static_call([$class, $method]); |
|
199 | + } |
|
200 | + } |
|
201 | + } |
|
202 | 202 | |
203 | - /** |
|
204 | - * Get the table qualified key name. |
|
205 | - * |
|
206 | - * @return string |
|
207 | - */ |
|
208 | - public function getQualifiedKeyName(): string |
|
209 | - { |
|
210 | - return $this->qualifyColumn($this->getKeyName()); |
|
211 | - } |
|
203 | + /** |
|
204 | + * Get the table qualified key name. |
|
205 | + * |
|
206 | + * @return string |
|
207 | + */ |
|
208 | + public function getQualifiedKeyName(): string |
|
209 | + { |
|
210 | + return $this->qualifyColumn($this->getKeyName()); |
|
211 | + } |
|
212 | 212 | |
213 | - /** |
|
214 | - * Qualify the given column name by the model's table. |
|
215 | - * |
|
216 | - * @param string $column |
|
217 | - * |
|
218 | - * @return string |
|
219 | - */ |
|
220 | - public function qualifyColumn($column): string |
|
221 | - { |
|
222 | - if (Str::contains($column, '.')) { |
|
223 | - return $column; |
|
224 | - } |
|
213 | + /** |
|
214 | + * Qualify the given column name by the model's table. |
|
215 | + * |
|
216 | + * @param string $column |
|
217 | + * |
|
218 | + * @return string |
|
219 | + */ |
|
220 | + public function qualifyColumn($column): string |
|
221 | + { |
|
222 | + if (Str::contains($column, '.')) { |
|
223 | + return $column; |
|
224 | + } |
|
225 | 225 | |
226 | - return $this->getTable().'.'.$column; |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * Get all of the models from the database. |
|
231 | - * |
|
232 | - * @param array|mixed $columns |
|
233 | - * |
|
234 | - * @return \Syscodes\Components\Database\Erostrine\Collection|static[] |
|
235 | - */ |
|
236 | - public static function all($columns = ['*']) |
|
237 | - { |
|
238 | - return static::query()->get( |
|
239 | - is_array($columns) ? $columns : func_get_args() |
|
240 | - ); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Begin querying the model on a given connection. |
|
245 | - * |
|
246 | - * @param string|null $connection |
|
247 | - * |
|
248 | - * @return \Syscodes\Components\Database\Erostrine\Builder |
|
249 | - */ |
|
250 | - public static function on($connection = null) |
|
251 | - { |
|
252 | - $instance = new static; |
|
253 | - $instance->setConnection($connection); |
|
254 | - |
|
255 | - return $instance->newQuery(); |
|
256 | - } |
|
226 | + return $this->getTable().'.'.$column; |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * Get all of the models from the database. |
|
231 | + * |
|
232 | + * @param array|mixed $columns |
|
233 | + * |
|
234 | + * @return \Syscodes\Components\Database\Erostrine\Collection|static[] |
|
235 | + */ |
|
236 | + public static function all($columns = ['*']) |
|
237 | + { |
|
238 | + return static::query()->get( |
|
239 | + is_array($columns) ? $columns : func_get_args() |
|
240 | + ); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Begin querying the model on a given connection. |
|
245 | + * |
|
246 | + * @param string|null $connection |
|
247 | + * |
|
248 | + * @return \Syscodes\Components\Database\Erostrine\Builder |
|
249 | + */ |
|
250 | + public static function on($connection = null) |
|
251 | + { |
|
252 | + $instance = new static; |
|
253 | + $instance->setConnection($connection); |
|
254 | + |
|
255 | + return $instance->newQuery(); |
|
256 | + } |
|
257 | 257 | |
258 | - /** |
|
259 | - * Get the primary key for the model. |
|
260 | - * |
|
261 | - * @return string |
|
262 | - */ |
|
263 | - public function getKeyName(): string |
|
264 | - { |
|
265 | - return $this->primaryKey; |
|
266 | - } |
|
258 | + /** |
|
259 | + * Get the primary key for the model. |
|
260 | + * |
|
261 | + * @return string |
|
262 | + */ |
|
263 | + public function getKeyName(): string |
|
264 | + { |
|
265 | + return $this->primaryKey; |
|
266 | + } |
|
267 | 267 | |
268 | - /** |
|
269 | - * Get the value of the model's primary key. |
|
270 | - * |
|
271 | - * @return mixed |
|
272 | - */ |
|
273 | - public function getKey() |
|
274 | - { |
|
275 | - return $this->getAttribute($this->getKeyName()); |
|
276 | - } |
|
268 | + /** |
|
269 | + * Get the value of the model's primary key. |
|
270 | + * |
|
271 | + * @return mixed |
|
272 | + */ |
|
273 | + public function getKey() |
|
274 | + { |
|
275 | + return $this->getAttribute($this->getKeyName()); |
|
276 | + } |
|
277 | 277 | |
278 | - /** |
|
279 | - * Set the primary key for the model. |
|
280 | - * |
|
281 | - * @param string $key |
|
282 | - * |
|
283 | - * @return void |
|
284 | - */ |
|
285 | - public function setKeyName($key): void |
|
286 | - { |
|
287 | - $this->primaryKey = $key; |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Update the model in the database. |
|
292 | - * |
|
293 | - * @param array $attributes |
|
294 | - * @param array $options |
|
295 | - * |
|
296 | - * @return bool |
|
297 | - */ |
|
298 | - public function update(array $attributes = [], array $options = []): bool |
|
299 | - { |
|
300 | - if ( ! $this->exists) { |
|
301 | - return false; |
|
302 | - } |
|
303 | - |
|
304 | - return $this->fill($attributes)->save($options); |
|
305 | - } |
|
306 | - |
|
307 | - /** |
|
308 | - * Save the model to the database. |
|
309 | - * |
|
310 | - * @param array $options |
|
311 | - * |
|
312 | - * @return bool |
|
313 | - */ |
|
314 | - public function save(array $options = []): bool |
|
315 | - { |
|
316 | - $query = $this->newQuery(); |
|
278 | + /** |
|
279 | + * Set the primary key for the model. |
|
280 | + * |
|
281 | + * @param string $key |
|
282 | + * |
|
283 | + * @return void |
|
284 | + */ |
|
285 | + public function setKeyName($key): void |
|
286 | + { |
|
287 | + $this->primaryKey = $key; |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Update the model in the database. |
|
292 | + * |
|
293 | + * @param array $attributes |
|
294 | + * @param array $options |
|
295 | + * |
|
296 | + * @return bool |
|
297 | + */ |
|
298 | + public function update(array $attributes = [], array $options = []): bool |
|
299 | + { |
|
300 | + if ( ! $this->exists) { |
|
301 | + return false; |
|
302 | + } |
|
303 | + |
|
304 | + return $this->fill($attributes)->save($options); |
|
305 | + } |
|
306 | + |
|
307 | + /** |
|
308 | + * Save the model to the database. |
|
309 | + * |
|
310 | + * @param array $options |
|
311 | + * |
|
312 | + * @return bool |
|
313 | + */ |
|
314 | + public function save(array $options = []): bool |
|
315 | + { |
|
316 | + $query = $this->newQuery(); |
|
317 | 317 | |
318 | - if ($this->fireModelEvent('saving') === false) { |
|
319 | - return false; |
|
320 | - } |
|
321 | - |
|
322 | - if ($this->exists) { |
|
323 | - $saved = $this->isDirty() ? $this->performUpdate($query) : true; |
|
324 | - } else { |
|
325 | - $saved = $this->performInsert($query); |
|
326 | - } |
|
327 | - |
|
328 | - if ($saved) { |
|
329 | - $this->fireModelEvent('saved', false); |
|
318 | + if ($this->fireModelEvent('saving') === false) { |
|
319 | + return false; |
|
320 | + } |
|
321 | + |
|
322 | + if ($this->exists) { |
|
323 | + $saved = $this->isDirty() ? $this->performUpdate($query) : true; |
|
324 | + } else { |
|
325 | + $saved = $this->performInsert($query); |
|
326 | + } |
|
327 | + |
|
328 | + if ($saved) { |
|
329 | + $this->fireModelEvent('saved', false); |
|
330 | 330 | |
331 | - $this->syncOriginal(); |
|
332 | - } |
|
333 | - |
|
334 | - return $saved; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * Perform a model update operation. |
|
339 | - * |
|
340 | - * @param \Syscodes\Components\Database\Erostrine\Builder $builder |
|
341 | - * |
|
342 | - * @return bool |
|
343 | - */ |
|
344 | - public function performUpdate(Builder $builder): bool |
|
345 | - { |
|
346 | - if ($this->fireModelEvent('updating') === false) { |
|
347 | - return false; |
|
348 | - } |
|
349 | - |
|
350 | - $dirty = $this->getDirty(); |
|
331 | + $this->syncOriginal(); |
|
332 | + } |
|
333 | + |
|
334 | + return $saved; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * Perform a model update operation. |
|
339 | + * |
|
340 | + * @param \Syscodes\Components\Database\Erostrine\Builder $builder |
|
341 | + * |
|
342 | + * @return bool |
|
343 | + */ |
|
344 | + public function performUpdate(Builder $builder): bool |
|
345 | + { |
|
346 | + if ($this->fireModelEvent('updating') === false) { |
|
347 | + return false; |
|
348 | + } |
|
349 | + |
|
350 | + $dirty = $this->getDirty(); |
|
351 | 351 | |
352 | - if (count($dirty) > 0) { |
|
353 | - $this->setKeysForSaveQuery($builder)->update($dirty); |
|
352 | + if (count($dirty) > 0) { |
|
353 | + $this->setKeysForSaveQuery($builder)->update($dirty); |
|
354 | 354 | |
355 | - $this->fireModelEvent('updated', false); |
|
356 | - } |
|
355 | + $this->fireModelEvent('updated', false); |
|
356 | + } |
|
357 | 357 | |
358 | - return true; |
|
359 | - } |
|
358 | + return true; |
|
359 | + } |
|
360 | 360 | |
361 | - /** |
|
362 | - * Set the keys for a save update query. |
|
363 | - * |
|
364 | - * @param \Syscodes\Components\Database\Erostrine\Builder $query |
|
365 | - * |
|
366 | - * @return \Syscodes\Components\Database\Erostrine\Builder |
|
367 | - */ |
|
368 | - protected function setKeysForSaveQuery($query) |
|
369 | - { |
|
370 | - $query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery()); |
|
361 | + /** |
|
362 | + * Set the keys for a save update query. |
|
363 | + * |
|
364 | + * @param \Syscodes\Components\Database\Erostrine\Builder $query |
|
365 | + * |
|
366 | + * @return \Syscodes\Components\Database\Erostrine\Builder |
|
367 | + */ |
|
368 | + protected function setKeysForSaveQuery($query) |
|
369 | + { |
|
370 | + $query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery()); |
|
371 | 371 | |
372 | - return $query; |
|
373 | - } |
|
372 | + return $query; |
|
373 | + } |
|
374 | 374 | |
375 | - /** |
|
376 | - * Get the primary key value for a save query. |
|
377 | - * |
|
378 | - * @return mixed |
|
379 | - */ |
|
380 | - protected function getKeyForSaveQuery() |
|
381 | - { |
|
382 | - return $this->original[$this->getKeyName()] ?? $this->getKey(); |
|
383 | - } |
|
384 | - |
|
385 | - /** |
|
386 | - * Perform a model insert operation. |
|
387 | - * |
|
388 | - * @param \Syscodes\Components\Database\Erostrine\Builder $builder |
|
389 | - * |
|
390 | - * @return bool |
|
391 | - */ |
|
392 | - public function performInsert(Builder $builder): bool |
|
393 | - { |
|
394 | - $attributes = $this->getAttributes(); |
|
395 | - |
|
396 | - if ($this->getIncrementing()) { |
|
397 | - $this->insertAndSetId($builder, $attributes); |
|
398 | - } else { |
|
399 | - if (empty($attributes)) { |
|
400 | - return true; |
|
401 | - } |
|
375 | + /** |
|
376 | + * Get the primary key value for a save query. |
|
377 | + * |
|
378 | + * @return mixed |
|
379 | + */ |
|
380 | + protected function getKeyForSaveQuery() |
|
381 | + { |
|
382 | + return $this->original[$this->getKeyName()] ?? $this->getKey(); |
|
383 | + } |
|
384 | + |
|
385 | + /** |
|
386 | + * Perform a model insert operation. |
|
387 | + * |
|
388 | + * @param \Syscodes\Components\Database\Erostrine\Builder $builder |
|
389 | + * |
|
390 | + * @return bool |
|
391 | + */ |
|
392 | + public function performInsert(Builder $builder): bool |
|
393 | + { |
|
394 | + $attributes = $this->getAttributes(); |
|
395 | + |
|
396 | + if ($this->getIncrementing()) { |
|
397 | + $this->insertAndSetId($builder, $attributes); |
|
398 | + } else { |
|
399 | + if (empty($attributes)) { |
|
400 | + return true; |
|
401 | + } |
|
402 | 402 | |
403 | - $builder->insert($attributes); |
|
404 | - } |
|
403 | + $builder->insert($attributes); |
|
404 | + } |
|
405 | 405 | |
406 | - $this->exists = true; |
|
406 | + $this->exists = true; |
|
407 | 407 | |
408 | - return true; |
|
409 | - } |
|
408 | + return true; |
|
409 | + } |
|
410 | 410 | |
411 | - /** |
|
412 | - * Insert the given attributes and set the ID on the model. |
|
413 | - * |
|
414 | - * @param \Syscodes\Components\Database\Erostrine\Builder $builder |
|
415 | - * @param array $attributes |
|
416 | - * |
|
417 | - * @return void |
|
418 | - */ |
|
419 | - protected function insertAndSetId(Builder $builder, $attributes) |
|
420 | - { |
|
421 | - $id = $builder->insertGetId($attributes, $keyName = $this->getKeyName()); |
|
411 | + /** |
|
412 | + * Insert the given attributes and set the ID on the model. |
|
413 | + * |
|
414 | + * @param \Syscodes\Components\Database\Erostrine\Builder $builder |
|
415 | + * @param array $attributes |
|
416 | + * |
|
417 | + * @return void |
|
418 | + */ |
|
419 | + protected function insertAndSetId(Builder $builder, $attributes) |
|
420 | + { |
|
421 | + $id = $builder->insertGetId($attributes, $keyName = $this->getKeyName()); |
|
422 | 422 | |
423 | - $this->setAttribute($keyName, $id); |
|
424 | - } |
|
425 | - |
|
426 | - /** |
|
427 | - * Delete the model from the database. |
|
428 | - * |
|
429 | - * @return bool|null |
|
430 | - */ |
|
431 | - public function delete() |
|
432 | - { |
|
433 | - if (is_null($this->getKeyName())) { |
|
434 | - throw new LogicException('No primary key defined on model'); |
|
435 | - } |
|
436 | - |
|
437 | - if ($this->fireModelEvent('deleting') === false) { |
|
438 | - return false; |
|
439 | - } |
|
440 | - |
|
441 | - if ( ! $this->exists) { |
|
442 | - return; |
|
443 | - } |
|
444 | - |
|
445 | - $this->performDeleteOnModel(); |
|
446 | - |
|
447 | - $this->fireModelEvent('deleted', false); |
|
448 | - |
|
449 | - return true; |
|
450 | - } |
|
423 | + $this->setAttribute($keyName, $id); |
|
424 | + } |
|
425 | + |
|
426 | + /** |
|
427 | + * Delete the model from the database. |
|
428 | + * |
|
429 | + * @return bool|null |
|
430 | + */ |
|
431 | + public function delete() |
|
432 | + { |
|
433 | + if (is_null($this->getKeyName())) { |
|
434 | + throw new LogicException('No primary key defined on model'); |
|
435 | + } |
|
436 | + |
|
437 | + if ($this->fireModelEvent('deleting') === false) { |
|
438 | + return false; |
|
439 | + } |
|
440 | + |
|
441 | + if ( ! $this->exists) { |
|
442 | + return; |
|
443 | + } |
|
444 | + |
|
445 | + $this->performDeleteOnModel(); |
|
446 | + |
|
447 | + $this->fireModelEvent('deleted', false); |
|
448 | + |
|
449 | + return true; |
|
450 | + } |
|
451 | 451 | |
452 | - /** |
|
453 | - * Perform the actual delete query on this model instance. |
|
454 | - * |
|
455 | - * @return static |
|
456 | - */ |
|
457 | - protected function performDeleteOnModel(): static |
|
458 | - { |
|
459 | - $this->setKeysForSaveQuery($this->newQuery())->delete(); |
|
452 | + /** |
|
453 | + * Perform the actual delete query on this model instance. |
|
454 | + * |
|
455 | + * @return static |
|
456 | + */ |
|
457 | + protected function performDeleteOnModel(): static |
|
458 | + { |
|
459 | + $this->setKeysForSaveQuery($this->newQuery())->delete(); |
|
460 | 460 | |
461 | - $this->exists = false; |
|
462 | - |
|
463 | - return $this; |
|
464 | - } |
|
465 | - |
|
466 | - /** |
|
467 | - * Delete the model from the database within a transaction. |
|
468 | - * |
|
469 | - * @return bool|null |
|
470 | - * |
|
471 | - * @throws \Throwable |
|
472 | - */ |
|
473 | - public function deleteOrFail() |
|
474 | - { |
|
475 | - if ( ! $this->exists) { |
|
476 | - return; |
|
477 | - } |
|
478 | - |
|
479 | - return $this->getConnection()->transaction(fn () => $this->delete()); |
|
480 | - } |
|
481 | - |
|
482 | - /** |
|
483 | - * Deleting the models for the given IDs. |
|
484 | - * |
|
485 | - * @param \Syscodes\Component\Collections\Collection|array|int|string $ids |
|
486 | - * |
|
487 | - * @return int |
|
488 | - */ |
|
489 | - public static function deleting($ids): int |
|
490 | - { |
|
491 | - $ids = is_array($ids) ? $ids : func_get_args(); |
|
492 | - |
|
493 | - if (count($ids) === 0) { |
|
494 | - return 0; |
|
495 | - } |
|
496 | - |
|
497 | - $key = ($instance = new static)->getKeyName(); |
|
498 | - |
|
499 | - $count = 0; |
|
500 | - |
|
501 | - foreach ($instance->whereIn($key, $ids)->get() as $model) { |
|
502 | - if ($model->delete()) { |
|
503 | - $count++; |
|
504 | - } |
|
505 | - } |
|
506 | - |
|
507 | - return $count; |
|
508 | - } |
|
461 | + $this->exists = false; |
|
462 | + |
|
463 | + return $this; |
|
464 | + } |
|
465 | + |
|
466 | + /** |
|
467 | + * Delete the model from the database within a transaction. |
|
468 | + * |
|
469 | + * @return bool|null |
|
470 | + * |
|
471 | + * @throws \Throwable |
|
472 | + */ |
|
473 | + public function deleteOrFail() |
|
474 | + { |
|
475 | + if ( ! $this->exists) { |
|
476 | + return; |
|
477 | + } |
|
478 | + |
|
479 | + return $this->getConnection()->transaction(fn () => $this->delete()); |
|
480 | + } |
|
481 | + |
|
482 | + /** |
|
483 | + * Deleting the models for the given IDs. |
|
484 | + * |
|
485 | + * @param \Syscodes\Component\Collections\Collection|array|int|string $ids |
|
486 | + * |
|
487 | + * @return int |
|
488 | + */ |
|
489 | + public static function deleting($ids): int |
|
490 | + { |
|
491 | + $ids = is_array($ids) ? $ids : func_get_args(); |
|
492 | + |
|
493 | + if (count($ids) === 0) { |
|
494 | + return 0; |
|
495 | + } |
|
496 | + |
|
497 | + $key = ($instance = new static)->getKeyName(); |
|
498 | + |
|
499 | + $count = 0; |
|
500 | + |
|
501 | + foreach ($instance->whereIn($key, $ids)->get() as $model) { |
|
502 | + if ($model->delete()) { |
|
503 | + $count++; |
|
504 | + } |
|
505 | + } |
|
506 | + |
|
507 | + return $count; |
|
508 | + } |
|
509 | 509 | |
510 | - /** |
|
511 | - * Get the value indicating whether the IDs are incrementing. |
|
512 | - * |
|
513 | - * @return bool |
|
514 | - */ |
|
515 | - public function getIncrementing(): bool |
|
516 | - { |
|
517 | - return $this->incrementing; |
|
510 | + /** |
|
511 | + * Get the value indicating whether the IDs are incrementing. |
|
512 | + * |
|
513 | + * @return bool |
|
514 | + */ |
|
515 | + public function getIncrementing(): bool |
|
516 | + { |
|
517 | + return $this->incrementing; |
|
518 | 518 | } |
519 | 519 | |
520 | - /** |
|
521 | - * Set the value indicating whether the IDs are incrementing. |
|
522 | - * |
|
523 | - * @param bool $value |
|
524 | - * |
|
525 | - * @return static |
|
526 | - */ |
|
527 | - public function setIncrementing($value): static |
|
528 | - { |
|
529 | - $this->incrementing = $value; |
|
520 | + /** |
|
521 | + * Set the value indicating whether the IDs are incrementing. |
|
522 | + * |
|
523 | + * @param bool $value |
|
524 | + * |
|
525 | + * @return static |
|
526 | + */ |
|
527 | + public function setIncrementing($value): static |
|
528 | + { |
|
529 | + $this->incrementing = $value; |
|
530 | 530 | |
531 | - return $this; |
|
532 | - } |
|
533 | - |
|
534 | - /** |
|
535 | - * Begin querying the model. |
|
536 | - * |
|
537 | - * @return \Syscodes\Components\Database\Erostrine\Builder |
|
538 | - */ |
|
539 | - public static function query() |
|
540 | - { |
|
541 | - return (new static)->newQuery(); |
|
542 | - } |
|
543 | - |
|
544 | - /** |
|
545 | - * Get a new query builder for the model's table. |
|
546 | - * |
|
547 | - * @return \Syscodes\Components\Database\Erostrine\Builder |
|
548 | - */ |
|
549 | - public function newQuery() |
|
550 | - { |
|
551 | - return $this->newQueryBuilder($this->newBaseQueryBuilder()) |
|
552 | - ->setModel($this) |
|
553 | - ->with($this->with); |
|
554 | - } |
|
555 | - |
|
556 | - /** |
|
557 | - * Create a new Erostrine query builder for the model. |
|
558 | - * |
|
559 | - * @param \Syscodes\Components\Database\Query\Builder $builder |
|
560 | - * |
|
561 | - * @return \Syscodes\Components\Database\Erostrine\Builder |
|
562 | - */ |
|
563 | - public function newQueryBuilder(QueryBuilder $builder) |
|
564 | - { |
|
565 | - return new Builder($builder); |
|
566 | - } |
|
531 | + return $this; |
|
532 | + } |
|
533 | + |
|
534 | + /** |
|
535 | + * Begin querying the model. |
|
536 | + * |
|
537 | + * @return \Syscodes\Components\Database\Erostrine\Builder |
|
538 | + */ |
|
539 | + public static function query() |
|
540 | + { |
|
541 | + return (new static)->newQuery(); |
|
542 | + } |
|
543 | + |
|
544 | + /** |
|
545 | + * Get a new query builder for the model's table. |
|
546 | + * |
|
547 | + * @return \Syscodes\Components\Database\Erostrine\Builder |
|
548 | + */ |
|
549 | + public function newQuery() |
|
550 | + { |
|
551 | + return $this->newQueryBuilder($this->newBaseQueryBuilder()) |
|
552 | + ->setModel($this) |
|
553 | + ->with($this->with); |
|
554 | + } |
|
555 | + |
|
556 | + /** |
|
557 | + * Create a new Erostrine query builder for the model. |
|
558 | + * |
|
559 | + * @param \Syscodes\Components\Database\Query\Builder $builder |
|
560 | + * |
|
561 | + * @return \Syscodes\Components\Database\Erostrine\Builder |
|
562 | + */ |
|
563 | + public function newQueryBuilder(QueryBuilder $builder) |
|
564 | + { |
|
565 | + return new Builder($builder); |
|
566 | + } |
|
567 | 567 | |
568 | - /** |
|
569 | - * Get a new query builder instance for the connection. |
|
570 | - * |
|
571 | - * @return \Syscodes\Components\Database\Query\Builder |
|
572 | - */ |
|
573 | - protected function newBaseQueryBuilder() |
|
574 | - { |
|
575 | - $connection = $this->getConnection(); |
|
576 | - |
|
577 | - $grammar = $connection->getQueryGrammar(); |
|
578 | - $processor = $connection->getPostProcessor(); |
|
579 | - |
|
580 | - return new QueryBuilder( |
|
581 | - $connection, $grammar, $processor |
|
582 | - ); |
|
583 | - } |
|
568 | + /** |
|
569 | + * Get a new query builder instance for the connection. |
|
570 | + * |
|
571 | + * @return \Syscodes\Components\Database\Query\Builder |
|
572 | + */ |
|
573 | + protected function newBaseQueryBuilder() |
|
574 | + { |
|
575 | + $connection = $this->getConnection(); |
|
576 | + |
|
577 | + $grammar = $connection->getQueryGrammar(); |
|
578 | + $processor = $connection->getPostProcessor(); |
|
579 | + |
|
580 | + return new QueryBuilder( |
|
581 | + $connection, $grammar, $processor |
|
582 | + ); |
|
583 | + } |
|
584 | 584 | |
585 | - /** |
|
586 | - * Fill the model with an array of attributes. |
|
587 | - * |
|
588 | - * @param array $attributes |
|
589 | - * |
|
590 | - * @return static |
|
591 | - * |
|
592 | - * @throws \Syscodes\Components\Database\Erostrine\Exceptions\MassAssignmentException |
|
593 | - */ |
|
594 | - public function fill(array $attributes): static |
|
595 | - { |
|
596 | - $totallyGuarded = $this->totallyGuarded(); |
|
585 | + /** |
|
586 | + * Fill the model with an array of attributes. |
|
587 | + * |
|
588 | + * @param array $attributes |
|
589 | + * |
|
590 | + * @return static |
|
591 | + * |
|
592 | + * @throws \Syscodes\Components\Database\Erostrine\Exceptions\MassAssignmentException |
|
593 | + */ |
|
594 | + public function fill(array $attributes): static |
|
595 | + { |
|
596 | + $totallyGuarded = $this->totallyGuarded(); |
|
597 | 597 | |
598 | - foreach ($this->fillableFromArray($attributes) as $key => $value) { |
|
599 | - $key = $this->removeTableFromKey($key); |
|
598 | + foreach ($this->fillableFromArray($attributes) as $key => $value) { |
|
599 | + $key = $this->removeTableFromKey($key); |
|
600 | 600 | |
601 | - if ($this->isFillable($key)) { |
|
602 | - $this->setAttribute($key, $value); |
|
603 | - } else if ($totallyGuarded) { |
|
604 | - throw new MassAssignmentException(sprintf( |
|
605 | - 'Add [%s] to fillable property to allow mass assignment on [%s]', |
|
606 | - $key, get_class($this) |
|
607 | - )); |
|
608 | - } |
|
609 | - } |
|
601 | + if ($this->isFillable($key)) { |
|
602 | + $this->setAttribute($key, $value); |
|
603 | + } else if ($totallyGuarded) { |
|
604 | + throw new MassAssignmentException(sprintf( |
|
605 | + 'Add [%s] to fillable property to allow mass assignment on [%s]', |
|
606 | + $key, get_class($this) |
|
607 | + )); |
|
608 | + } |
|
609 | + } |
|
610 | 610 | |
611 | - return $this; |
|
612 | - } |
|
611 | + return $this; |
|
612 | + } |
|
613 | 613 | |
614 | - /** |
|
615 | - * Create a new instance of the given model. |
|
616 | - * |
|
617 | - * @param array $attributes |
|
618 | - * @param bool $exists |
|
619 | - * |
|
620 | - * @return static |
|
621 | - */ |
|
622 | - public function newInstance($attributes = [], $exists = false): static |
|
623 | - { |
|
614 | + /** |
|
615 | + * Create a new instance of the given model. |
|
616 | + * |
|
617 | + * @param array $attributes |
|
618 | + * @param bool $exists |
|
619 | + * |
|
620 | + * @return static |
|
621 | + */ |
|
622 | + public function newInstance($attributes = [], $exists = false): static |
|
623 | + { |
|
624 | 624 | |
625 | - $model = new static; |
|
625 | + $model = new static; |
|
626 | 626 | |
627 | - $model->exists = $exists; |
|
627 | + $model->exists = $exists; |
|
628 | 628 | |
629 | - $model->setConnection($this->getConnectionName()); |
|
629 | + $model->setConnection($this->getConnectionName()); |
|
630 | 630 | |
631 | - $model->setTable($this->getTable()); |
|
631 | + $model->setTable($this->getTable()); |
|
632 | 632 | |
633 | - $model->fill((array) $attributes); |
|
633 | + $model->fill((array) $attributes); |
|
634 | 634 | |
635 | - return $model; |
|
636 | - } |
|
635 | + return $model; |
|
636 | + } |
|
637 | 637 | |
638 | - /** |
|
639 | - * Create a new model instance that is existing. |
|
640 | - * |
|
641 | - * @param array $attributes |
|
642 | - * @param string|null $connection |
|
643 | - * |
|
644 | - * @return static |
|
645 | - */ |
|
646 | - public function newFromBuilder($attributes = [], $connection = null): static |
|
647 | - { |
|
648 | - $instance = $this->newInstance([], true); |
|
649 | - $instance->setRawAttributes((array) $attributes, true); |
|
650 | - $instance->setConnection($connection ?: $this->getConnectionName()); |
|
638 | + /** |
|
639 | + * Create a new model instance that is existing. |
|
640 | + * |
|
641 | + * @param array $attributes |
|
642 | + * @param string|null $connection |
|
643 | + * |
|
644 | + * @return static |
|
645 | + */ |
|
646 | + public function newFromBuilder($attributes = [], $connection = null): static |
|
647 | + { |
|
648 | + $instance = $this->newInstance([], true); |
|
649 | + $instance->setRawAttributes((array) $attributes, true); |
|
650 | + $instance->setConnection($connection ?: $this->getConnectionName()); |
|
651 | 651 | |
652 | - return $instance; |
|
653 | - } |
|
654 | - |
|
655 | - /** |
|
656 | - * Create a new ORM Collection instance. |
|
657 | - * |
|
658 | - * @param array $models |
|
659 | - * |
|
660 | - * @return \Syscodes\Components\Database\Collection |
|
661 | - */ |
|
662 | - public function newCollection(array $models = []) |
|
663 | - { |
|
664 | - return new Collection($models); |
|
665 | - } |
|
652 | + return $instance; |
|
653 | + } |
|
654 | + |
|
655 | + /** |
|
656 | + * Create a new ORM Collection instance. |
|
657 | + * |
|
658 | + * @param array $models |
|
659 | + * |
|
660 | + * @return \Syscodes\Components\Database\Collection |
|
661 | + */ |
|
662 | + public function newCollection(array $models = []) |
|
663 | + { |
|
664 | + return new Collection($models); |
|
665 | + } |
|
666 | 666 | |
667 | - /** |
|
668 | - * Create a new pivot model instance. |
|
669 | - * |
|
670 | - * @param \Syscodes\Components\Database\Erostrine\Model $parent |
|
671 | - * @param array $attributes |
|
672 | - * @param string $table |
|
673 | - * @param bool $exists |
|
674 | - * @param string|null $using |
|
675 | - * |
|
676 | - * @return \Syscodes\Components\Database\Eloquent\Relations\Pivot |
|
677 | - */ |
|
678 | - public function newPivot(self $parent, array $attributes, $table, $exists, $using = null) |
|
679 | - { |
|
680 | - return $using ? $using::fromRawAttributes($parent, $attributes, $table, $exists) |
|
681 | - : Pivot::fromAttributes($parent, $attributes, $table, $exists); |
|
682 | - } |
|
683 | - |
|
684 | - /** |
|
685 | - * Get the table associated with the model. |
|
686 | - * |
|
687 | - * @return string |
|
688 | - */ |
|
689 | - public function getTable(): string |
|
690 | - { |
|
691 | - $class = class_basename($this); |
|
692 | - |
|
693 | - return $this->table ?? Str::snake(Str::plural($class)); |
|
694 | - } |
|
695 | - |
|
696 | - /** |
|
697 | - * Set the table associated with the model. |
|
698 | - * |
|
699 | - * @param string $table |
|
700 | - * |
|
701 | - * @return void |
|
702 | - */ |
|
703 | - public function setTable(string $table): void |
|
704 | - { |
|
705 | - $this->table = $table; |
|
706 | - } |
|
707 | - |
|
708 | - /** |
|
709 | - * Get the default foreign key name for the model. |
|
710 | - * |
|
711 | - * @return string |
|
712 | - */ |
|
713 | - public function getForeignKey(): string |
|
714 | - { |
|
715 | - $name = class_basename($this); |
|
716 | - |
|
717 | - return sprintf("%s_{$this->getKeyName()}", Str::snake($name)); |
|
718 | - } |
|
719 | - |
|
720 | - /** |
|
721 | - * Get the instance as an array. |
|
667 | + /** |
|
668 | + * Create a new pivot model instance. |
|
669 | + * |
|
670 | + * @param \Syscodes\Components\Database\Erostrine\Model $parent |
|
671 | + * @param array $attributes |
|
672 | + * @param string $table |
|
673 | + * @param bool $exists |
|
674 | + * @param string|null $using |
|
675 | + * |
|
676 | + * @return \Syscodes\Components\Database\Eloquent\Relations\Pivot |
|
677 | + */ |
|
678 | + public function newPivot(self $parent, array $attributes, $table, $exists, $using = null) |
|
679 | + { |
|
680 | + return $using ? $using::fromRawAttributes($parent, $attributes, $table, $exists) |
|
681 | + : Pivot::fromAttributes($parent, $attributes, $table, $exists); |
|
682 | + } |
|
683 | + |
|
684 | + /** |
|
685 | + * Get the table associated with the model. |
|
686 | + * |
|
687 | + * @return string |
|
688 | + */ |
|
689 | + public function getTable(): string |
|
690 | + { |
|
691 | + $class = class_basename($this); |
|
692 | + |
|
693 | + return $this->table ?? Str::snake(Str::plural($class)); |
|
694 | + } |
|
695 | + |
|
696 | + /** |
|
697 | + * Set the table associated with the model. |
|
698 | + * |
|
699 | + * @param string $table |
|
700 | + * |
|
701 | + * @return void |
|
702 | + */ |
|
703 | + public function setTable(string $table): void |
|
704 | + { |
|
705 | + $this->table = $table; |
|
706 | + } |
|
707 | + |
|
708 | + /** |
|
709 | + * Get the default foreign key name for the model. |
|
710 | + * |
|
711 | + * @return string |
|
712 | + */ |
|
713 | + public function getForeignKey(): string |
|
714 | + { |
|
715 | + $name = class_basename($this); |
|
716 | + |
|
717 | + return sprintf("%s_{$this->getKeyName()}", Str::snake($name)); |
|
718 | + } |
|
719 | + |
|
720 | + /** |
|
721 | + * Get the instance as an array. |
|
722 | 722 | * |
723 | 723 | * @return array |
724 | - */ |
|
725 | - public function toArray(): array |
|
726 | - { |
|
727 | - $attributes = array_diff_key($this->attributes, array_flip($this->hidden)); |
|
724 | + */ |
|
725 | + public function toArray(): array |
|
726 | + { |
|
727 | + $attributes = array_diff_key($this->attributes, array_flip($this->hidden)); |
|
728 | 728 | |
729 | - foreach ($this->relations as $name => $value) { |
|
730 | - if (in_array($name, $this->hidden)) { |
|
731 | - continue; |
|
732 | - } |
|
729 | + foreach ($this->relations as $name => $value) { |
|
730 | + if (in_array($name, $this->hidden)) { |
|
731 | + continue; |
|
732 | + } |
|
733 | 733 | |
734 | - $key = Str::snake($name); |
|
734 | + $key = Str::snake($name); |
|
735 | 735 | |
736 | - if (is_array($value)) { |
|
737 | - $attributes[$key] = []; |
|
736 | + if (is_array($value)) { |
|
737 | + $attributes[$key] = []; |
|
738 | 738 | |
739 | - foreach ($value as $id => $model) { |
|
740 | - $attributes[$key][$id] = $model->toArray(); |
|
741 | - } |
|
742 | - } |
|
743 | - // The value is not an array of models |
|
744 | - else if ($value instanceof Arrayable) { |
|
745 | - $attributes[$key] = $value->toArray(); |
|
746 | - } else if (is_null($value)) { |
|
747 | - $attributes[$key] = $value; |
|
748 | - } |
|
749 | - } |
|
739 | + foreach ($value as $id => $model) { |
|
740 | + $attributes[$key][$id] = $model->toArray(); |
|
741 | + } |
|
742 | + } |
|
743 | + // The value is not an array of models |
|
744 | + else if ($value instanceof Arrayable) { |
|
745 | + $attributes[$key] = $value->toArray(); |
|
746 | + } else if (is_null($value)) { |
|
747 | + $attributes[$key] = $value; |
|
748 | + } |
|
749 | + } |
|
750 | 750 | |
751 | - return $attributes; |
|
752 | - } |
|
751 | + return $attributes; |
|
752 | + } |
|
753 | 753 | |
754 | - /** |
|
755 | - * Remove the table name from a given key. |
|
756 | - * |
|
757 | - * @param string $key |
|
758 | - * |
|
759 | - * @return string |
|
760 | - */ |
|
761 | - protected function removeTableFromKey($key): string |
|
762 | - { |
|
763 | - if ( ! Str::contains($key, '.')) { |
|
764 | - return $key; |
|
765 | - } |
|
754 | + /** |
|
755 | + * Remove the table name from a given key. |
|
756 | + * |
|
757 | + * @param string $key |
|
758 | + * |
|
759 | + * @return string |
|
760 | + */ |
|
761 | + protected function removeTableFromKey($key): string |
|
762 | + { |
|
763 | + if ( ! Str::contains($key, '.')) { |
|
764 | + return $key; |
|
765 | + } |
|
766 | 766 | |
767 | - return lastItem(explode('.', $key)); |
|
768 | - } |
|
769 | - |
|
770 | - /** |
|
771 | - * Determine if two models have the same ID and belong to the same table. |
|
772 | - * |
|
773 | - * @param \Syscodes\Components\Database\Erostrine\Model|null $model |
|
774 | - * |
|
775 | - * @return bool |
|
776 | - */ |
|
777 | - public function is($model): bool |
|
778 | - { |
|
779 | - return ! is_null($model) && |
|
780 | - $this->getKey() === $model->getKey() && |
|
781 | - $this->getTable() === $model->getTable() && |
|
782 | - $this->getConnectionName() === $model->getConnectionName(); |
|
783 | - } |
|
784 | - |
|
785 | - /** |
|
786 | - * Determine if two models are not the same. |
|
787 | - * |
|
788 | - * @param \Syscodes\Components\Database\Erostrine\Model|null $model |
|
789 | - * |
|
790 | - * @return bool |
|
791 | - */ |
|
792 | - public function isNot($model): bool |
|
793 | - { |
|
794 | - return ! $this->is($model); |
|
795 | - } |
|
767 | + return lastItem(explode('.', $key)); |
|
768 | + } |
|
769 | + |
|
770 | + /** |
|
771 | + * Determine if two models have the same ID and belong to the same table. |
|
772 | + * |
|
773 | + * @param \Syscodes\Components\Database\Erostrine\Model|null $model |
|
774 | + * |
|
775 | + * @return bool |
|
776 | + */ |
|
777 | + public function is($model): bool |
|
778 | + { |
|
779 | + return ! is_null($model) && |
|
780 | + $this->getKey() === $model->getKey() && |
|
781 | + $this->getTable() === $model->getTable() && |
|
782 | + $this->getConnectionName() === $model->getConnectionName(); |
|
783 | + } |
|
784 | + |
|
785 | + /** |
|
786 | + * Determine if two models are not the same. |
|
787 | + * |
|
788 | + * @param \Syscodes\Components\Database\Erostrine\Model|null $model |
|
789 | + * |
|
790 | + * @return bool |
|
791 | + */ |
|
792 | + public function isNot($model): bool |
|
793 | + { |
|
794 | + return ! $this->is($model); |
|
795 | + } |
|
796 | 796 | |
797 | - /** |
|
798 | - * Get the database connection for the model. |
|
799 | - * |
|
800 | - * return \Syscodes\Components\Database\Database\Connection |
|
801 | - */ |
|
802 | - public function getConnection() |
|
803 | - { |
|
804 | - return static::resolveConnection($this->getConnectionName()); |
|
805 | - } |
|
797 | + /** |
|
798 | + * Get the database connection for the model. |
|
799 | + * |
|
800 | + * return \Syscodes\Components\Database\Database\Connection |
|
801 | + */ |
|
802 | + public function getConnection() |
|
803 | + { |
|
804 | + return static::resolveConnection($this->getConnectionName()); |
|
805 | + } |
|
806 | 806 | |
807 | - /** |
|
808 | - * Get the current connection name for the model. |
|
809 | - * |
|
810 | - * @return string |
|
811 | - */ |
|
812 | - public function getConnectionName() |
|
813 | - { |
|
814 | - return $this->connection; |
|
815 | - } |
|
807 | + /** |
|
808 | + * Get the current connection name for the model. |
|
809 | + * |
|
810 | + * @return string |
|
811 | + */ |
|
812 | + public function getConnectionName() |
|
813 | + { |
|
814 | + return $this->connection; |
|
815 | + } |
|
816 | 816 | |
817 | - /** |
|
818 | - * Set the connection associated with the model. |
|
819 | - * |
|
820 | - * @param string $name |
|
821 | - * |
|
822 | - * @return static |
|
823 | - */ |
|
824 | - public function setConnection($name): static |
|
825 | - { |
|
826 | - $this->connection = $name; |
|
817 | + /** |
|
818 | + * Set the connection associated with the model. |
|
819 | + * |
|
820 | + * @param string $name |
|
821 | + * |
|
822 | + * @return static |
|
823 | + */ |
|
824 | + public function setConnection($name): static |
|
825 | + { |
|
826 | + $this->connection = $name; |
|
827 | 827 | |
828 | - return $this; |
|
829 | - } |
|
830 | - |
|
831 | - /** |
|
832 | - * The resolver connection a instance. |
|
833 | - * |
|
834 | - * @param string|null $connection |
|
835 | - * |
|
836 | - * @return \Syscodes\Components\Database\Connections\Connection |
|
837 | - */ |
|
838 | - public static function resolveConnection(string $connection = null) |
|
839 | - { |
|
840 | - return static::$resolver->connection($connection); |
|
841 | - } |
|
842 | - |
|
843 | - /** |
|
844 | - * Get the connectiion resolver instance. |
|
845 | - * |
|
846 | - * @return \Syscodes\Components\Database\ConnectionResolverInstance |
|
847 | - */ |
|
848 | - public static function getConnectionResolver() |
|
849 | - { |
|
850 | - return static::$resolver; |
|
851 | - } |
|
852 | - |
|
853 | - /** |
|
854 | - * Set the connection resolver instance. |
|
855 | - * |
|
856 | - * @param \Syscodes\Components\Database\ConnectionResolverInstance $resolver |
|
857 | - * |
|
858 | - * @return void |
|
859 | - */ |
|
860 | - public static function setConnectionResolver(ConnectionResolverInterface $resolver): void |
|
861 | - { |
|
862 | - static::$resolver = $resolver; |
|
863 | - } |
|
864 | - |
|
865 | - /** |
|
866 | - * Magic method. |
|
867 | - * |
|
868 | - * Dynamically retrieve attributes on the model. |
|
869 | - * |
|
870 | - * @param string $key |
|
871 | - * |
|
872 | - * @return mixed |
|
873 | - */ |
|
874 | - public function __get($key) |
|
875 | - { |
|
876 | - return $this->getAttribute($key); |
|
877 | - } |
|
878 | - |
|
879 | - /** |
|
880 | - * Magic method. |
|
881 | - * |
|
882 | - * Dynamically set attributes on the model. |
|
883 | - * |
|
884 | - * @param string $key |
|
885 | - * @param mixed $value |
|
886 | - * |
|
887 | - * @return void |
|
888 | - */ |
|
889 | - public function __set($key, $value) |
|
890 | - { |
|
891 | - return $this->setAttribute($key, $value); |
|
892 | - } |
|
893 | - |
|
894 | - /* |
|
828 | + return $this; |
|
829 | + } |
|
830 | + |
|
831 | + /** |
|
832 | + * The resolver connection a instance. |
|
833 | + * |
|
834 | + * @param string|null $connection |
|
835 | + * |
|
836 | + * @return \Syscodes\Components\Database\Connections\Connection |
|
837 | + */ |
|
838 | + public static function resolveConnection(string $connection = null) |
|
839 | + { |
|
840 | + return static::$resolver->connection($connection); |
|
841 | + } |
|
842 | + |
|
843 | + /** |
|
844 | + * Get the connectiion resolver instance. |
|
845 | + * |
|
846 | + * @return \Syscodes\Components\Database\ConnectionResolverInstance |
|
847 | + */ |
|
848 | + public static function getConnectionResolver() |
|
849 | + { |
|
850 | + return static::$resolver; |
|
851 | + } |
|
852 | + |
|
853 | + /** |
|
854 | + * Set the connection resolver instance. |
|
855 | + * |
|
856 | + * @param \Syscodes\Components\Database\ConnectionResolverInstance $resolver |
|
857 | + * |
|
858 | + * @return void |
|
859 | + */ |
|
860 | + public static function setConnectionResolver(ConnectionResolverInterface $resolver): void |
|
861 | + { |
|
862 | + static::$resolver = $resolver; |
|
863 | + } |
|
864 | + |
|
865 | + /** |
|
866 | + * Magic method. |
|
867 | + * |
|
868 | + * Dynamically retrieve attributes on the model. |
|
869 | + * |
|
870 | + * @param string $key |
|
871 | + * |
|
872 | + * @return mixed |
|
873 | + */ |
|
874 | + public function __get($key) |
|
875 | + { |
|
876 | + return $this->getAttribute($key); |
|
877 | + } |
|
878 | + |
|
879 | + /** |
|
880 | + * Magic method. |
|
881 | + * |
|
882 | + * Dynamically set attributes on the model. |
|
883 | + * |
|
884 | + * @param string $key |
|
885 | + * @param mixed $value |
|
886 | + * |
|
887 | + * @return void |
|
888 | + */ |
|
889 | + public function __set($key, $value) |
|
890 | + { |
|
891 | + return $this->setAttribute($key, $value); |
|
892 | + } |
|
893 | + |
|
894 | + /* |
|
895 | 895 | |----------------------------------------------------------------- |
896 | 896 | | ArrayAccess Methods |
897 | 897 | |----------------------------------------------------------------- |
898 | 898 | */ |
899 | 899 | |
900 | - /** |
|
901 | - * Whether or not an offset exists. |
|
902 | - * |
|
903 | - * @param string $offset |
|
904 | - * |
|
905 | - * @return bool |
|
906 | - */ |
|
907 | - public function offsetExists($offset): bool |
|
908 | - { |
|
909 | - return isset($this->attributes[$offset]) || |
|
910 | - isset($this->relations[$offset]) || |
|
911 | - $this->hasGetMutator($offset) && |
|
912 | - ! is_null($this->getAttributeValue($offset)); |
|
913 | - } |
|
914 | - |
|
915 | - /** |
|
916 | - * Returns the value at specified offset. |
|
917 | - * |
|
918 | - * @param string $offset |
|
919 | - * |
|
920 | - * @return mixed |
|
921 | - */ |
|
922 | - public function offsetGet($offset) |
|
923 | - { |
|
924 | - return $this->getAttribute($offset); |
|
925 | - } |
|
926 | - |
|
927 | - /** |
|
928 | - * Set a value to the specified offset |
|
929 | - * |
|
930 | - * @param string $offset |
|
931 | - * @param mixed $value |
|
932 | - * |
|
933 | - * @return void |
|
934 | - */ |
|
935 | - public function offsetSet($offset, $value): void |
|
936 | - { |
|
937 | - $this->setAttribute($offset, $value); |
|
938 | - } |
|
939 | - |
|
940 | - /** |
|
941 | - * Unset the value for a given offset. |
|
942 | - * |
|
943 | - * @param string $offset |
|
944 | - * |
|
945 | - * @return void |
|
946 | - */ |
|
947 | - public function offsetUnset($offset): void |
|
948 | - { |
|
949 | - unset($this->attributes[$offset], $this->relations[$offset]); |
|
950 | - } |
|
951 | - |
|
952 | - /** |
|
953 | - * Determine if an attribute or relation exists on the model. |
|
954 | - * |
|
955 | - * @param string $key |
|
956 | - * |
|
957 | - * @return bool |
|
958 | - */ |
|
959 | - public function __isset($key) |
|
960 | - { |
|
961 | - return $this->offsetExists($key); |
|
962 | - } |
|
900 | + /** |
|
901 | + * Whether or not an offset exists. |
|
902 | + * |
|
903 | + * @param string $offset |
|
904 | + * |
|
905 | + * @return bool |
|
906 | + */ |
|
907 | + public function offsetExists($offset): bool |
|
908 | + { |
|
909 | + return isset($this->attributes[$offset]) || |
|
910 | + isset($this->relations[$offset]) || |
|
911 | + $this->hasGetMutator($offset) && |
|
912 | + ! is_null($this->getAttributeValue($offset)); |
|
913 | + } |
|
914 | + |
|
915 | + /** |
|
916 | + * Returns the value at specified offset. |
|
917 | + * |
|
918 | + * @param string $offset |
|
919 | + * |
|
920 | + * @return mixed |
|
921 | + */ |
|
922 | + public function offsetGet($offset) |
|
923 | + { |
|
924 | + return $this->getAttribute($offset); |
|
925 | + } |
|
926 | + |
|
927 | + /** |
|
928 | + * Set a value to the specified offset |
|
929 | + * |
|
930 | + * @param string $offset |
|
931 | + * @param mixed $value |
|
932 | + * |
|
933 | + * @return void |
|
934 | + */ |
|
935 | + public function offsetSet($offset, $value): void |
|
936 | + { |
|
937 | + $this->setAttribute($offset, $value); |
|
938 | + } |
|
939 | + |
|
940 | + /** |
|
941 | + * Unset the value for a given offset. |
|
942 | + * |
|
943 | + * @param string $offset |
|
944 | + * |
|
945 | + * @return void |
|
946 | + */ |
|
947 | + public function offsetUnset($offset): void |
|
948 | + { |
|
949 | + unset($this->attributes[$offset], $this->relations[$offset]); |
|
950 | + } |
|
951 | + |
|
952 | + /** |
|
953 | + * Determine if an attribute or relation exists on the model. |
|
954 | + * |
|
955 | + * @param string $key |
|
956 | + * |
|
957 | + * @return bool |
|
958 | + */ |
|
959 | + public function __isset($key) |
|
960 | + { |
|
961 | + return $this->offsetExists($key); |
|
962 | + } |
|
963 | 963 | |
964 | - /** |
|
965 | - * Unset an attribute on the model. |
|
966 | - * |
|
967 | - * @param string $key |
|
968 | - * |
|
969 | - * @return void |
|
970 | - */ |
|
971 | - public function __unset($key) |
|
972 | - { |
|
973 | - $this->offsetUnset($key); |
|
974 | - } |
|
964 | + /** |
|
965 | + * Unset an attribute on the model. |
|
966 | + * |
|
967 | + * @param string $key |
|
968 | + * |
|
969 | + * @return void |
|
970 | + */ |
|
971 | + public function __unset($key) |
|
972 | + { |
|
973 | + $this->offsetUnset($key); |
|
974 | + } |
|
975 | 975 | |
976 | - /** |
|
977 | - * Magic method. |
|
978 | - * |
|
979 | - * Dynamically handle method calls into the model instance. |
|
980 | - * |
|
981 | - * @param string $method |
|
982 | - * @param array $parameters |
|
983 | - * |
|
984 | - * @return mixed |
|
985 | - */ |
|
986 | - public function __call($method, $parameters) |
|
987 | - { |
|
988 | - return $this->forwardCallTo($this->newQuery(), $method, $parameters); |
|
989 | - } |
|
976 | + /** |
|
977 | + * Magic method. |
|
978 | + * |
|
979 | + * Dynamically handle method calls into the model instance. |
|
980 | + * |
|
981 | + * @param string $method |
|
982 | + * @param array $parameters |
|
983 | + * |
|
984 | + * @return mixed |
|
985 | + */ |
|
986 | + public function __call($method, $parameters) |
|
987 | + { |
|
988 | + return $this->forwardCallTo($this->newQuery(), $method, $parameters); |
|
989 | + } |
|
990 | 990 | |
991 | - /** |
|
992 | - * Magic method. |
|
993 | - * |
|
994 | - * Dynamically handle static method calls into the model instance. |
|
995 | - * |
|
996 | - * @param string $method |
|
997 | - * @param array $parameters |
|
998 | - * |
|
999 | - * @return mixed |
|
1000 | - */ |
|
1001 | - public static function __callStatic($method, $parameters) |
|
1002 | - { |
|
1003 | - return (new static)->{$method}(...$parameters); |
|
1004 | - } |
|
991 | + /** |
|
992 | + * Magic method. |
|
993 | + * |
|
994 | + * Dynamically handle static method calls into the model instance. |
|
995 | + * |
|
996 | + * @param string $method |
|
997 | + * @param array $parameters |
|
998 | + * |
|
999 | + * @return mixed |
|
1000 | + */ |
|
1001 | + public static function __callStatic($method, $parameters) |
|
1002 | + { |
|
1003 | + return (new static)->{$method}(...$parameters); |
|
1004 | + } |
|
1005 | 1005 | } |
1006 | 1006 | \ No newline at end of file |
@@ -177,7 +177,7 @@ |
||
177 | 177 | */ |
178 | 178 | protected function newRelatedInstance($class) |
179 | 179 | { |
180 | - return take(new $class, function ($instance) { |
|
180 | + return take(new $class, function($instance) { |
|
181 | 181 | if ( ! $instance->getConnectionName()) { |
182 | 182 | $instance->setConnection($this->connection); |
183 | 183 | } |
@@ -39,11 +39,11 @@ |
||
39 | 39 | protected $attributes = []; |
40 | 40 | |
41 | 41 | /** |
42 | - * The model attribute's original state. |
|
43 | - * |
|
44 | - * @var array $original |
|
45 | - */ |
|
46 | - protected $original = []; |
|
42 | + * The model attribute's original state. |
|
43 | + * |
|
44 | + * @var array $original |
|
45 | + */ |
|
46 | + protected $original = []; |
|
47 | 47 | |
48 | 48 | /** |
49 | 49 | * Indicates whether attributes are snake cased on arrays. |
@@ -44,11 +44,11 @@ |
||
44 | 44 | protected $observables = []; |
45 | 45 | |
46 | 46 | /** |
47 | - * The event dispatcher instance. |
|
48 | - * |
|
49 | - * @var \Syscodes\Components\Contracts\Events\Dispatcher $dispatcher |
|
50 | - */ |
|
51 | - protected static $dispatcher; |
|
47 | + * The event dispatcher instance. |
|
48 | + * |
|
49 | + * @var \Syscodes\Components\Contracts\Events\Dispatcher $dispatcher |
|
50 | + */ |
|
51 | + protected static $dispatcher; |
|
52 | 52 | |
53 | 53 | /** |
54 | 54 | * Get the observable event names. |