Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RonB1985's avatar

Pivot get selected values

I have three tables, products, lists and list_product. They are related via belongsToMany.

What I want to be able to do is, create a new list, and then display all products in a table with checkboxes. Then, I select a few, and save them, which will call a sync method to store the list_id and all product_id in the pivot table.

So far, so good, no issues here whatsoever. If I would simply have one list, I wouldn't have any issues editing the list. What I cannot figure out is, when editing a list, how can I make sure that all products from the pivot table, are selected in the list, in my edit form?

The thing is, when editing a list, I also want to be able to select more products to add them to the list. So I basically will retrieve all products in my edit form as well, and I then want to be able to have the ones that are present in the pivot table, to be selected.

0 likes
0 replies

Please or to participate in this conversation.