Skip to content

Commit

Permalink
[11.x] Allow easier overriding of the exception thrown by invalid ID …
Browse files Browse the repository at this point in the history
…in route binding (#53944)

* allow customizing the exception raised by invalid unique IDs

* remove types

* Update HasUniqueStringIds.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
cosmastech and taylorotwell authored Dec 17, 2024
1 parent 32b6824 commit b6f5f02
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Illuminate/Database/Eloquent/Concerns/HasUniqueStringIds.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

0 comments on commit b6f5f02

Please sign in to comment.