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

jaddz's avatar
Level 1

Declaring and using ModelNotFoundException

Hi All,

I'm following along with the Laravel from scratch tutorials and finding it extremely useful. Challenging at times as I am new to a lot of the languages and tools used (php, laravel, composer, artisan etc). I have installed laravel 9 with php 8.2 on apache 2.4 on an AWS ubuntu AMI. Everything has been working fantastically so far but there has been the odd one or two things that I have had to work my way through doing some reading on. But I have come across an issue that I dont seem to be able to get past.

In Section 3 -> Blade -> "A few tweaks and considerations" jeffrey uses a class called 'ModelNotFoundException" When i try to call this class I get an error -> Class "App\Models\ModelNotFoundException" not found

I am calling the class from a script in app/models called Post.php.

I have added a use statement at the top of that script

use Illuminate\Database\Eloquent\ModelNotFoundException;

I am using two other classes within the same block of code (learned through the tutorial) which work fine.

The File:files class and method and the YamlFrontMatter::parsefiles class and method. Both have been referenced with a similar use statement at the beginning of the code.

use Illuminate\Support\Facades\File;

use Spatie\YamlFrontMatter\YamlFrontMatter;

I have also tried using this ModelNotFoundException class in my web.php routes file and it works fine when I put the use Illuminate\Database\Eloquent\ModelNotFoundException; statement at the top of that file.

<?php

namespace App\Models;
use Illuminate\Support\Facades\File;
use Spatie\YamlFrontMatter\YamlFrontMatter;
use Illuminate\Database\Eloquent\ModelNotFoundException;


class Post {

    public $title;
    public $excerpt;
    public $date;
    public $body;
    public $slug;

    public function __construct($title, $excerpt, $date, $body, $slug)
    {
        $this->title = $title;
        $this->excerpt = $excerpt;
        $this->date = $date;
        $this->body = $body;
        $this->slug = $slug;
    }

    public static function all()
    {
        throw new ModelNotFoundExecption();
        return cache()->rememberForever('posts.all', function (){
           return collect(File::files(resource_path("posts")))
                ->map(fn($file) => YamlFrontMatter::parsefile($file))
                ->map(fn($document) => new Post(
                    $document->title,
                    $document->excerpt,
                    $document->date,
                    $document->body(),
                    $document->slug
                ))
                ->sortByDesc('date');
        });
    }

    public static function find($slug)
    {
        
        $post = static::all()->firstWhere('slug', $slug);
        return $post;
        // if (! $post) {
        //     throw new ModelNotFoundExecption();
        // }
        
    }
}

Any help would be gratefully received I am assuming it has something to do with the namespace as the error is suggesting that App\Models\ModelNotFoundException cannot be found. But of course thats the class namespace I am already in.

0 likes
6 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

There is a missing letter

ModelNotFoundExecption() //yours 
ModelNotFoundExeception()
jaddz's avatar
Level 1

Im so dumb........ Thanks for helping me spell words correctly Sinnbeck.

Sinnbeck's avatar

@jaddz happy to help. It's one of those errors that we have all had and have been staring on for hours. My trick was to copy the actual name and search the page. I noticed that it didn't highlight but uses of the word.

Please mark a best answer to set the thread as solved

jaddz's avatar
Level 1

@Sinnbeck Yep I spent a lot of time on this one. But learnt during that time so no harm done really. Thanks again and Best Answer has been marked.

Please or to participate in this conversation.