简体   繁体   中英

LARAVEL | Many-To-Many Relationship Between Three Tables - saving data from two arrays

I hope someone with advanced experience with PHP/Laravel and database design can guide me in the right direction with this. I have the following database relationships and have the models for all but the pivot tables.

Database model screenshot

My issue is that I am unable to relate a Subcategory with at least ONE or MANY (not necessarily all) of its Suppliers to a Project. I would like to save this kind of record into the project_subcategory table. I can easily save this manually into the DB, but since I'm getting the data from two different arrays from my view (Subcategories and Suppliers), I'm having a hard time saving properly into the DB.

Example of data inserted in the project_subcategory pivot table

I know that many-to-many relationships should be between two tables only and that the many-to-many polymorphic approach suggested by Laravel docs might seem to be a solution for this at a first glance; however, after trying this approach, I don't think it can work in my case and that's why I'm asking here – after spending many hours trying to solve this issue and doing lots of research.

Again, all I need to be able to do is assign many subcategories and one or many of its suppliers to a project. In other words, a project can have many Subcategories assigned to it and each subcategory can have many suppliers assigned to it. This list of suppliers is generated from the subcategory_supplier table.

Please let me know if something does not make sense or if I need to explain this further.

Any help will be much appreciated!

PS: View example of what I need to achieve

I've managed to find a solution to my problem - therefore, my original question is answered. I'm leaving here what I've done to solve the issue in the hope that it might be helpful to someone facing a similar scenario.

What I've done is rather than trying to insert from two different arrays (Subcategories and Suppliers), I combined them into a single array from the view. Then by using foreach loops I extracted the subcategory ID (key) and the suppliers IDs (values). Then using the Laravel 'attach' method I inserted into the project_subcategory table.

//Insert records to the project_subcategory table.
        if ($request->subcategories_suppliers != null) {
            //dd($request->subcategories_suppliers);

            //The array containing the subcategories as the array key and the suppliers ids as the array value
            $array = $request->subcategories_suppliers;

            //Detach all records from project_subcategory before updating
            $project->subcategories()->detach();
            //Loop throuh $array to obtain the id for the subcategory and the array with all the suppliers ids
            foreach ($array as $subcategory => $suppliers) {
                //Loop through $suppliers to get each supplier's id
                foreach ($suppliers as $supplierID) {
                    //Attach values into the pivot table project_subcategory
                    $project->subcategories()->attach([$subcategory => ['supplier_supplierID' => $supplierID]]);
                }
            }

This is some of the code from the html blade view:

 @foreach($subcategory->suppliers as $supplier)
                                        <div class="form-check d-none suppliers-list{{$subcategory->subcategoryID}}"
                                            id="">
                                            <input type="hidden" name="subcategory_name"
                                                value="{{$subcategory->subcategoryID}}" />
                                            <input class="form-check-input" type="checkbox"
                                                value="{{$supplier->supplierID}}"
                                                **name="subcategories_suppliers[{{$subcategory->subcategoryID}}][]"**
                                                @if(in_array($supplier->supplierID,
                                            old('subcategories_suppliers',
                                            $project->subcategories()->pluck('supplier_supplierID')->toArray())) && in_array($subcategory->subcategoryID, $project->subcategories()->pluck('subcategory_subcategoryID')->toArray()))
                                            checked
                                            @endif>
                                            <label class="form-check-label" for="defaultCheck1">
                                                <b>{{$supplier->supplier_name}}</b>
                                            </label>
                                        </div>
                                        @continue
                                        @endforeach

Hope this might help someone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM