I'm new to Laravel, could you push me into the right direction?
I need to render blog post on url like this site.com/posts/1/slugged-url-of-the-post/
, where 1
is the category_id
.
I have no problem with site.com/posts/slugged-url-of-the-post/
, but absolutely stuck with adding one more parameter to the route and making it work. The database is okay, and I have no problems with displaying the post category_id as a text value, for example, in the posts.show blade file.
Please, help?
routes/web.php
Route::get('/posts/{post}', function( App\Models\Post $post) {
// need to make this work:
// Route::get('/posts/{category_id}/{post}' ...
return view('posts.show')->with('post', $post);
});
app/Models/Post.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;
use Cviebrock\EloquentSluggable\SluggableScopeHelpers;
class Post extends Model
{
use HasFactory, Sluggable, SluggableScopeHelpers;
public function sluggable(): array
{
return [
'slug' => [
'source' => 'title',
],
];
}
public function getRouteKeyName(): string
{
return 'slug';
}
}
CodePudding user response: