Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PRESTIGE2930's avatar

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
}
0 likes
2 replies
tisuchi's avatar

@prestige2930 Do you need an automatic injection?

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)
    }
}
PRESTIGE2930's avatar

@tisuchi would have done this, but yes i think i need an automatic injection because $regNo is not the id column but a foreignId column

1 like

Please or to participate in this conversation.