spatie / sheets

Store & retrieve your static content in plain text files
https://spatie.be/open-source
MIT License
273 stars 21 forks source link

Can't get single sheet when using custom pathParser #64

Closed Hyra closed 3 years ago

Hyra commented 3 years ago

When using a custom pathParser, for instance the default Spatie\Sheets\PathParsers\SlugWithDateParser it lists the parsed slugs perfectly.

However, when requesting the single version by this slug it won't find it in the collection.

For instance, a post called 2020-01-30.some-post-here.md and the following sheets config:

    'collections' => [
        'posts' => [
            'path_parser' => Spatie\Sheets\PathParsers\SlugWithDateParser::class,
        ]

This will list the slug as some-post-here, but when navigating to /blog/some-post-here and using the following route:

Route::get('/blog/{slug}', function (string $slug, Sheets $sheets) {

    $article = $sheets->collection('posts')->get($slug);
    dd($article); // Will be null, it expects the original full path including the timestamp
});

It will result to null because get seems to expect the full path again, rather than the parsed slug. Requesting it with /blog/2020-01-30.some-post-here works fine, but defeats the purpose of the original slug.

Am i missing something, or is this intentionally?

olssonm commented 3 years ago

You are on the right track, using ->get($slug) you are still querying for the path.

Try this instead:

$article = $sheets->collection('posts')
    ->all() // This returns a Collection-instance
    ->where('slug', $slug)
    ->first();

You could also look into the route model binding (docs) and bind it with the above resolution-logic.

After writing this I realize your issue is quite old, hopefully you solved it!

Hyra commented 3 years ago

Hi @olssonm thanks for your reply.

I ended up solving it like this back then:

    $article = $sheets->collection('posts')->all()->first(function ($item) use ($slug) {
        return $item->slug === $slug;
    });

I like your approach better though, it's a lot cleaner :)

spatie-bot commented 3 years ago

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.