P
P
Petya Fox2019-06-03 19:49:53
Laravel
Petya Fox, 2019-06-03 19:49:53

How to display category slug in Laravel post?

Guys, how to display the category slug in a post in Laravel so that the link is like this: domain/category/post-name. I searched everywhere - both in Runet and in the bourgeoisie, but I did not find anything.
If you know, please show an example or at least throw off a link if you also encountered a similar problem.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
jazzus, 2019-06-03
@fullstackru

Form a slug
Write to the database, make a route
route in template
In the controller, you take arguments and look for a post by slug builder->where('slug',$slug)->firstOrFail();
parameters in routes

N
NubasLol, 2019-06-03
@NubasLol

$post->category->slug

P
Petya Fox, 2019-11-23
@fullstackru

I made it much easier:

public function posts()
{
    return $this->morphedByMany('App\Post', 'category_able');
}

$category = App\Category::first();
$category->posts();

{{ route('post', $post->slug) }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question