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
– Miguel
This Edit would not show the record on the screen before the change?
public function edit(Post $post)
?– novic
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
– Fábio Jânio