lundi 29 octobre 2018

Update pivot table to create dynamic menu(menu and sub items) - Laravel

The case is : I want to make a dynamic sidebar when the user choose a book from the list -I used checkboxes -it should be saved in a pivot table with the category id so this will let me to to generate a dynamic sidebar for each user for example:

user 1 sidebar

- category 1

book 1 book 2

- category 2

book1 book2

My tables :

 user_book
-----------------------------------------
 user_id| book_id|category _id
-----------------------------------------


user_categories
-----------------------------------------
 user_id| book_id|category _id
-----------------------------------------


 books
-----------------------------------------
id| book_name|category_id|
-----------------------------------------

categories
-----------------------------------------
id| category|
-----------------------------------------

my code:

$user->books()->sync($request->input('books_ids'));// to save books ids -array of ids- but don't know how to insert the related category for each book in the pivot table.

and to generate my sidebar:

 $user->categories()->with('books')->get();

so my questions are : 1. is this the right way to make a dynamic sidebar and what is the best way? 2. how can I insert the related category in the pivot table ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire