77use ArrayAccess ;
88use Bavix \LaravelClickHouse \Database \Connection ;
99use Bavix \LaravelClickHouse \Database \Query \Builder as QueryBuilder ;
10+ use Illuminate \Contracts \Events \Dispatcher ;
11+ use Illuminate \Contracts \Routing \UrlRoutable ;
1012use Illuminate \Contracts \Support \Arrayable ;
1113use Illuminate \Contracts \Support \Jsonable ;
14+ use Illuminate \Database \ConnectionInterface ;
1215use Illuminate \Database \ConnectionResolverInterface ;
1316use Illuminate \Database \ConnectionResolverInterface as Resolver ;
1417use Illuminate \Database \Eloquent \Concerns \GuardsAttributes ;
1720use Illuminate \Database \Eloquent \Concerns \HidesAttributes ;
1821use Illuminate \Database \Eloquent \JsonEncodingException ;
1922use Illuminate \Database \Eloquent \MassAssignmentException ;
23+ use Illuminate \Database \Eloquent \Relations \BelongsToMany ;
24+ use Illuminate \Database \Eloquent \Relations \HasManyThrough ;
25+ use Illuminate \Database \Eloquent \Relations \Relation ;
2026use Illuminate \Support \Str ;
2127use JsonSerializable ;
2228use Tinderbox \ClickhouseBuilder \Query \Grammar ;
2329
24- abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
30+ abstract class Model implements ArrayAccess, UrlRoutable, Arrayable, Jsonable, JsonSerializable
2531{
2632 use Concerns \HasAttributes;
2733 use Concerns \Common;
@@ -35,7 +41,7 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab
3541 *
3642 * @var bool
3743 */
38- public $ exists = false ;
44+ public bool $ exists = false ;
3945
4046 /**
4147 * Indicates if an exception should be thrown when trying to access a missing attribute on a retrieved model.
@@ -93,19 +99,9 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab
9399 */
94100 protected $ perPage = 15 ;
95101
96- /**
97- * The connection resolver instance.
98- *
99- * @var Resolver
100- */
101- protected static $ resolver ;
102+ protected static ConnectionResolverInterface $ resolver ;
102103
103- /**
104- * The event dispatcher instance.
105- *
106- * @var \Illuminate\Contracts\Events\Dispatcher
107- */
108- protected static $ dispatcher ;
104+ protected static Dispatcher $ dispatcher ;
109105
110106 /**
111107 * The array of booted models.
@@ -114,6 +110,8 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab
114110 */
115111 protected static $ booted = [];
116112
113+ public bool $ wasRecentlyCreated = false ;
114+
117115 /**
118116 * Create a new Eloquent model instance.
119117 */
@@ -381,7 +379,7 @@ public function jsonSerialize(): array
381379 /**
382380 * Get the database connection for the model.
383381 */
384- public function getConnection (): \ Illuminate \ Database \ Connection
382+ public function getConnection (): ConnectionInterface
385383 {
386384 return static ::resolveConnection ($ this ->getConnectionName ());
387385 }
@@ -408,10 +406,8 @@ public function setConnection(string $name)
408406
409407 /**
410408 * Resolve a connection instance.
411- *
412- * @return Connection|\Illuminate\Database\ConnectionInterface
413409 */
414- public static function resolveConnection (string $ connection = null )
410+ public static function resolveConnection (string $ connection = null ): ConnectionInterface
415411 {
416412 return static ::getConnectionResolver ()->connection ($ connection );
417413 }
@@ -456,6 +452,52 @@ public function setTable(string $table): self
456452 return $ this ;
457453 }
458454
455+ public function resolveRouteBinding ($ value , $ field = null ): ?Model
456+ {
457+ return $ this ->resolveRouteBindingQuery ($ this , $ value , $ field )->first ();
458+ }
459+
460+ public function resolveSoftDeletableRouteBinding (mixed $ value , ?string $ field = null ): ?Model
461+ {
462+ return $ this ->resolveRouteBindingQuery ($ this , $ value , $ field )->withTrashed ()->first ();
463+ }
464+
465+ public function resolveChildRouteBinding ($ childType , $ value , $ field ): ?Model
466+ {
467+ return $ this ->resolveChildRouteBindingQuery ($ childType , $ value , $ field )->first ();
468+ }
469+
470+ public function resolveSoftDeletableChildRouteBinding (string $ childType , mixed $ value , ?string $ field = null ): ?Model
471+ {
472+ return $ this ->resolveChildRouteBindingQuery ($ childType , $ value , $ field )->withTrashed ()->first ();
473+ }
474+
475+ protected function resolveChildRouteBindingQuery (string $ childType , mixed $ value , ?string $ field = null ): Relation |Model
476+ {
477+ $ relationship = $ this ->{$ this ->childRouteBindingRelationshipName ($ childType )}();
478+
479+ $ field = $ field ?: $ relationship ->getRelated ()->getRouteKeyName ();
480+
481+ if ($ relationship instanceof HasManyThrough ||
482+ $ relationship instanceof BelongsToMany) {
483+ $ field = $ relationship ->getRelated ()->getTable ().'. ' .$ field ;
484+ }
485+
486+ return $ relationship instanceof Model
487+ ? $ relationship ->resolveRouteBindingQuery ($ relationship , $ value , $ field )
488+ : $ relationship ->getRelated ()->resolveRouteBindingQuery ($ relationship , $ value , $ field );
489+ }
490+
491+ protected function childRouteBindingRelationshipName (string $ childType ): string
492+ {
493+ return Str::plural (Str::camel ($ childType ));
494+ }
495+
496+ public function resolveRouteBindingQuery (Relation |Model $ query , mixed $ value , ?string $ field = null ): Builder
497+ {
498+ return $ query ->where ($ field ?? $ this ->getRouteKeyName (), $ value );
499+ }
500+
459501 /**
460502 * Get the primary key for the model.
461503 */
@@ -504,6 +546,16 @@ public function setKeyType(string $type)
504546 return $ this ;
505547 }
506548
549+ public function getRouteKey (): mixed
550+ {
551+ return $ this ->getAttribute ($ this ->getRouteKeyName ());
552+ }
553+
554+ public function getRouteKeyName (): string
555+ {
556+ return $ this ->getKeyName ();
557+ }
558+
507559 /**
508560 * Get the value of the model's primary key.
509561 *
@@ -533,9 +585,9 @@ public function getPerPage(): int
533585 /**
534586 * Set the number of models to return per page.
535587 *
536- * @return $this
588+ * @return static
537589 */
538- public function setPerPage (int $ perPage ): self
590+ public function setPerPage (int $ perPage ): static
539591 {
540592 $ this ->perPage = $ perPage ;
541593
0 commit comments