Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Allow easier overriding of the exception thrown by invalid ID in route binding #53944

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/Illuminate/Database/Eloquent/Concerns/HasUniqueStringIds.php
Original file line number Diff line number Diff line change
@@ -54,11 +54,11 @@ public function uniqueIds()
public function resolveRouteBindingQuery($query, $value, $field = null)
{
if ($field && in_array($field, $this->uniqueIds()) && ! $this->isValidUniqueId($value)) {
throw (new ModelNotFoundException)->setModel(get_class($this), $value);
$this->handleInvalidUniqueId($value, $field);
}

if (! $field && in_array($this->getRouteKeyName(), $this->uniqueIds()) && ! $this->isValidUniqueId($value)) {
throw (new ModelNotFoundException)->setModel(get_class($this), $value);
$this->handleInvalidUniqueId($value, $field);
}

return parent::resolveRouteBindingQuery($query, $value, $field);
@@ -91,4 +91,18 @@ public function getIncrementing()

return $this->incrementing;
}

/**
* Throw an exception for the given invalid unique ID.
*
* @param mixed $value
* @param string|null $field
* @return never
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
protected function handleInvalidUniqueId($value, $field)
{
throw (new ModelNotFoundException)->setModel(get_class($this), $value);
Copy link
Contributor

@shaedrich shaedrich Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could even put the exception name as static property on the model so ModelNotFoundException is taken as a default but can be overwritten without overwriting handleInvalidUniqueId() when one just wants to change the class name

Suggested change
throw (new ModelNotFoundException)->setModel(get_class($this), $value);
throw (new $notFoundException)->setModel(get_class($this), $value);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea, but I think it would require the class to implement setModel() which isn't enforced by an interface.

Copy link
Contributor

@shaedrich shaedrich Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point 👍🏻 Maybe this can be implemented via an interface then 🤔

 trait HasUniqueStringIds
 {
+    private ModelException $notFoundException = ModelNotFoundException
interface ModelException
{
    /**
     * Set the affected Eloquent model and instance ids.
     *
     * @param  class-string<TModel>  $model
     * @param  array<int, int|string>|int|string  $ids
     * @return $this
     */
    public function setModel($model, $ids = []);
}

}
}