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

agahi's avatar
Level 1

handling the "No query results for model" error

Hey everybody

On my recent pages I decided it's time to go pro :) and since I might not know the correct terminology I bring the code I have tried.

This is my route: The element parameter is actually the primary Id on the table

Route::get('element/{element}/{something}','ElementsController@show');

And this is the method:

    public function show(Element $element)

As long as there is a row on the table with that id every thing works fantastic but if the record has been deleted for some reasons I get:


NotFoundHttpException in Handler.php line 113:
No query results for model [App\Element].

On my normal pages - ie not pro style - I would have sent the id rather than the "element" and checked the existence of the id and would redirect the page to search based on {something} part but apparently the error here is thrown somewhere between requesting the route and starting the method. What would you wise guys suggest I should do?

By the way I searched the forum and found similar questions but somehow they didn't help.

0 likes
2 replies
matt_panton's avatar

Have you tried removing the Element typehint in the show method to prevent the implicit model binding.

public function show($elementId) {
    if($el = Element::find($elementId) {

    } else {
        // redirect to search here
    }
}
curtiplas's avatar

Hi @agahi not sure if you found the issue or not. But essentially that error is saying that it can't find anything in the table for the App\Element model with the id matching the $element item you sent.

Essentially the error is trying to tell you that nothing in the table you're pointing it at matches.

That said I'm trying to do the same thing you are I believe which is...

pass along the id or other Route Key Name to the show function on a controller. However, i want to be able to redirect the user to a different partial/page if nothing is matches the query it runs.

Would love to know if you got around it.

Please or to participate in this conversation.