How to recover categories linked to a post?

Asked

Viewed 309 times

4

I climbed on Github (https://github.com/fabiojaniolima/larablog-beta) a small project in Laravel, something very initial. I intend as soon as possible to arrange the routes, structure the methods, etc.

I released this code prematurely because I came across a problem that I am not able to solve alone. I explain below.

I set up a scheme of Categories, this scheme will be used by the Library screen, Courses and Posts. I’m already able to register categories and also link a post to more than one category, as well as undo the relationship (post <=> category) when deleting the post. The problem is at the time of editing a post, I’m not able to at this time reassemble the whole structure of categories pre-selecting those linked to the post.

The method used to edit the post is located in App Http Controllers Postcontroller Panel:

public function edit(Post $post)
{
    $categorias = Categoria::all()->toHierarchy();

    return view('painel.posts.cad-edit', compact('post', 'categorias'));
}

I’ve only known Laravel a short time, so I don’t know how he works very well. At the end of this project I will release the same on an MIT license, as well as use this to make available some of my books, articles and courses for free.

The idea of categories is something like: Imgur.com/a/itX71

  • From what I’ve seen in git hub, you have a many-to-many relationship between post and categories, this will help: https://laravel.com/docs/5.4/eloquent-relationships#Many-to-Many, https://laravel.com/docs/5.4/eloquent-relationships#updating-Many-to-Many-relationships . The first link helps you know how to get the posts in each category (and vice versa) and the second is to update that relationship, with attach/detach

  • This Edit would not show the record on the screen before the change? public function edit(Post $post)?

  • Yes. I want to list the post, as well as the category to which it is associated and all other registered categories, allowing the "author" to change the associated category. Something similar to what exists in Wordpress: http://imgur.com/a/itX71

1 answer

1

If you want to list the categories related to the post. You can call the model method that says who are the categories associated with it.

Type:

public function edit(Post $post)
{
    $categorias = Post::find($id)->categorias;

    return view('painel.posts.cad-edit', compact('post', 'categorias'));
}

this way it will return the categories related to this post in specific!

  • I want to list all but marking the related.

  • So what can be done is you return all categories with Categories:all() generating an array of all categories and make a check if the categories returned from the Post are contained in each other and make a mark, this can be done already directly in the view, you return the lists and pimbas! You can use Php in_array!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.