Failed Query is Not throwing ModelNotFoundException
I have this function that fires an event function that retrieves a user.
note:
(a . there is a reason it's an event and not just calling the function directly.
b. this is a Livewire controller)
If the user is not found/ the dependency injection fail, I get this " No query results for model [App\Models\User] PISADM954U2023 " on the debug screen,
but according to the Laravel documentation, if a user is not found it supposed to throw a ModelNotFoundException , with 403 status code, but it doesn't do that.
even if I turn of debug mode, i get a 500 status page.
any solution.
public function mount(Request $request)
{
if($request->has('regNo'))
{
$this->fireEvent('updatedProfile', [$request->query('regNo')], 1);
}
};
public function updatedProfile (User $reg)
{
$this->regNo = $reg['registration_number'];
// more
}
Otherwise, I suggest this approach which is a bit manual:
public function updatedProfile($regNo)
{
try {
$user = User::where('registration_number', $regNo)->firstOrFail();
$this->regNo = $user->registration_number;
// more
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
// Handle the exception (e.g., return a 404 response or a custom view)
}
}