Skip to content

Commit 20e4561

Browse files
committed
refactor(database): add back resolve to models
1 parent 8d91c2b commit 20e4561

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

packages/database/src/Builder/ModelQueryBuilder.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@ public function findById(string|int|PrimaryKey $id): object
145145
return $this->get($id);
146146
}
147147

148+
/**
149+
* Finds a model instance by its ID.
150+
*
151+
* **Example**
152+
* ```php
153+
* model(User::class)->resolve(1);
154+
* ```
155+
*
156+
* @return TModel
157+
*/
158+
public function resolve(string|int|PrimaryKey $id): object
159+
{
160+
if (! inspect($this->model)->hasPrimaryKey()) {
161+
throw ModelDidNotHavePrimaryColumn::neededForMethod($this->model, 'resolve');
162+
}
163+
164+
return $this->get($id);
165+
}
166+
148167
/**
149168
* Gets a model instance by its ID, optionally loading the given relationships.
150169
*

packages/database/src/IsDatabaseModel.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ public static function new(mixed ...$params): self
5959
*/
6060
public static function findById(string|int|PrimaryKey $id): static
6161
{
62-
return self::resolve($id);
62+
return self::get($id);
63+
}
64+
65+
/**
66+
* Finds a model instance by its ID.
67+
*/
68+
public static function resolve(string|int|PrimaryKey $id): static
69+
{
70+
return model(self::class)->resolve($id);
6371
}
6472

6573
/**

0 commit comments

Comments
 (0)