diglactic / laravel-breadcrumbs

Laravel Breadcrumbs - A simple Laravel-style way to create breadcrumbs.
https://packagist.org/packages/diglactic/laravel-breadcrumbs
MIT License
868 stars 63 forks source link

Can we use a date string instead of post id in route url? #68

Closed adam-godfrey closed 1 year ago

adam-godfrey commented 1 year ago

Everything is working fine but this is just a query to see if we can us a date instead of an id to get the post.

I want a url like example.com/blog/2023-05-31/this-is-a-test-post. I've seen it done on other sites but not sure if it can be done with tis pacakge.

A recap...

{{ Breadcrumbs::render('blog.show', $blog) }}

use App\Models\Blog;
use Diglactic\Breadcrumbs\Breadcrumbs;
use Diglactic\Breadcrumbs\Generator as BreadcrumbTrail;

// /Home > Blog
Breadcrumbs::for('blog.show', function (BreadcrumbTrail $trail, Blog $blog) {
    $trail->parent('blog.index');
    $trail->push($blog->title, route('blog.show', $blog));
}); 
public function show(Blog $blog)
{
        $blog->content = GeneralHelper::nl2p($blog->content);

        $pageSize = 6;
        $entries = Blog::all(); 
        $entries = CollectionHelper::paginate($entries, $pageSize);

        $popular = $entries->sortByDesc('views')
            ->take(5);

        $data = array(
            'blog' => $blog,
            'entries' => $entries,
            'popular' => $popular
        );

        return view('awents/blog-single')->with($data); 
}
Route::get('/blog', [BlogController::class, 'index'])->name('blog.index');
Route::get('/blog/{blog}/{slug?}', [BlogController::class, 'show'])->name('blog.show');
shengslogar commented 1 year ago

You may run into trouble with the non-uniqueness of date strings, but this is ultimately a Laravel problem. This package will play nice with whatever model Laravel throws its way.

Now that you're using Laravel's route model binding, you'll just need to figure out how to "resolve" your blog post model via your desired parameters.

See https://laravel.com/docs/10.x/routing#customizing-the-resolution-logic