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

@kasparslie's avatar

Blog tutorial

Good afternoon, Everybody! I am learning Laravel and I went through the tutorial and now I am a bit stuck, so in short I defined class Post and which has a function All and function Find. in both cases when I dd returns I get all the information I need, but then when I call my class Post::find($slug) from routes/web.php I return Null. Has anybody got any ideas?

namespace App\Models;

use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\File;
use Spatie\YamlFrontMatter\YamlFrontMatter;
use Illuminate\Support\Facades\Cache;


class Post
{

    public $title;

    public $slug;

    public $date;

    public $excerpt;

    public $body;


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

       $this -> body = $body;

       $this -> date =  $date;

       $this -> excerpt = $excerpt;

       $this -> slug = $slug;


    }

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

        });

    }
    public static function find($slug)
    {
        $posts = Post::all();
        $posts -> firstWhere('slug', $slug);



    }

}
use App\Models\Post;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\File;


Route::get('/', function () {
    return view ('posts', [
        'posts' => Post::all()
    ]);

    });

Route::get('posts/{post}', function ($slug) {
        dd(Post::find($slug));
        return view ('post', [
        'post' => Post::find($slug)
    ]);

})->where('post', '[A-z_\-]+');

0 likes
12 replies
Sinnbeck's avatar

Show some code. We cannot possibly guess the error by a small description

1 like
MooseSaid's avatar

I don't remember that Jeffery user 'matter("date"),' in the Laravel from scratch course!

Sinnbeck's avatar

Great. The problem is that you arent using the same variable name

Route::get('posts/{slug}', function ($slug) { //changed so both are named slug
        dd(Post::find($slug));
        return view ('post', [
        'post' => Post::find($slug)
    ]);

})->where('post', '[A-z_\-]+');
1 like
@kasparslie's avatar

@Sinnbeck hmmm.... changed my code, cleared cache, and refresh the page: /posts/my-third-post and the response I get are following:

// 20220222120033 localhost

null

Sinnbeck's avatar

@@kasparslie it does not seem slug is the primary key? What tutorial is this?

dd(Post::where('slug', $slug)->first());
1 like
@kasparslie's avatar

@Sinnbeck Thank you for reply, it is this one /series/laravel-8-from-scratch/episodes/12 Unfortunately, my code collapsed because I don't have Function Where defined in my Post class.

Sinnbeck's avatar

@@kasparslie Ahh sorry missed you had made it yourself. The problem is a missing return then. Go back to how your code was

public static function find($slug)
    {
        $posts = Post::all();
        return $posts->firstWhere('slug', $slug); //return the result



    }
1 like
Sinnbeck's avatar

@@kasparslie Happy to help and sorry for the confusion. I wrongly assumed it was eloquent.

1 like

Please or to participate in this conversation.