'Insert all documents as per selected departments and so on

I'm trying to insert Department wise Doc in Docuploadrole table. For first department, all documents which are related to that department will be inserted and then will go for next department to do perform the same until loops ends for all departments.

Below is the screenshot of the input page: enter image description here

Below is my desired output:

enter image description here

Controller

/*Insert into DB*/
public function DocRoleUploadRoleStore(Request $request)
    {
        $uploadroledoctype = $request->uploadroledoctype;
        $uploadroledept = $request->uploadroledept;
        foreach($uploadroledoctype as $docid) {
                     Docuploadrole::create([
                    'deptid' => $request->uploadroledept,
                    'docid' => $docid,
                    'username' => $request->uploadroleuser,
                ]);
         }
        return redirect()->back()->with('status','Upload Role Added successfully');
    }

/*Document Type based on Department Dependency*/  
public function DocRoleGetUploadRoleDocType(Request $request){
        $cid = $request -> post('cid');
        $GetUploadRoleDocRole = DB:: table('departments')
        ->join('doc_types','departments.deptid', '=', 'doc_types.deptid')
            ->whereIn('departments.deptid',explode(",", $cid))
            ->get();
            $html = ' ';
            foreach($GetUploadRoleDocRole as $list)
            $html.= '<option  value="'.$list ->docid.' '.$list ->deptid.'">'.$list ->deptid.' - '.$list ->docname.'</option>';

        echo $html;

    }

Blade View

<form action="/document-upload-role-data" method="post">
                    @csrf
                    <div class="row">
                        <div class="col-lg-3">
                            <select class="form-select form-control" name="uploadroleuser">
                                <option selected>Select User</option>
                                @foreach ($usersdata as $item)
                                    <option value="{{$item->username}}">{{$item->username}}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-lg-3 d-flex">
                            <select class="form-select form-control" name="uploadroledept[]" multiple id="upload-role-dept">
                                <option selected>Select Department</option>
                                @foreach ($departmentDatas as $item)
                                    <option value="{{$item->deptid}}">{{$item->deptname}}</option>
                                @endforeach
                            </select>
                           
                            <input class="mx-1" style="height: 28px;" type="checkbox" name="selectalldepartment" id="upload-role-all-dept"><small style="margin-top: 2px;">All Dept.</small>          
                        </div>
                        <div class="col-lg-6 d-flex">
                            <select class="form-select form-control" id="upload-role-doctype" type="checkbox" name="uploadroledoctype[]" multiple>
                                {{-- <option type="checkbox" selected="false">Document Type</option> --}}
                            </select>
                            <input class="mx-1" style="height: 28px;" type="checkbox" name="select_all" id="upload-role-all-doctype-selected"><small style="margin-top: 2px;">All Doc.</small>
                        </div>
                        <div class="col-lg-12">
                            <button type="submit" class="btn btn-primary float-right mt-4">Submit</button>
                        </div>
                    </div>
                </form>

Route

Route::post('/document-upload-role-data'
[DocumentRoleController::class,'DocRoleUploadRoleStore']);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source