laravel 删除一个对象并按顺序重命名所有对象名称

bsxbgnwa  于 2023-01-10  发布在  其他
关注(0)|答案(1)|浏览(117)
    • 对象**

这是我检索到的对象。我如何删除module_1对象并重命名模块对象,例如删除module_1并将module_2、module_3 ...重命名为module_1 module_2 ...。

`{
  "module_1": {
    "modulename": "maths",
    "moduletypes": "mandatory",
    "moduleid": "88",
    "moduleaim": "MODULE AIM",
    "learningobjectives": "LEARNING OBJECTIVES",
    "assessmentcriteria": "SSESSMENT CRITERIA",
    "scenario": "sfgsfgsdfg",
    "tasknumber": 2,
    "taskarea": [
      "<p>taskarea1sdfgsdfsfgagagasdasdf</p>",
      "<p>asdasdasd</p>"
    ],
    "librarylinks": [
      "asdasdsa",
      "asdasd"
    ],
    "studylinks": [
      "asdasd",
      "asdasd"
    ],
    "videoslinks": [
      "asdasdfsdfsdfsdfsdfsdfsdf",
      "asdasdfsfsdfsf"
    ]
  },
  "module_2": {
    "modulename": "asasdasd",
    "moduletypes": "optional",
    "moduleid": "23443",
    "moduleaim": "asdasgdfg",
    "learningobjectives": "dgdg",
    "assessmentcriteria": "dfgdg",
    "scenario": "dgdgdg",
    "tasknumber": 1,
    "taskarea": [
      "dgdfgdfg"
    ],
    "librarylinks": [
      "dfgdfgdfg"
    ],
    "studylinks": [
      "dgdfgdfg"
    ],
    "videoslinks": [
      "dgdfgdfg",
      "sdfasdfasf"
    ]
  },
  "module_3": {
    "modulename": "science",
    "moduletypes": "optional",
    "moduleid": "3234",
    "moduleaim": "sadfsdfgsdaf",
    "learningobjectives": "asfdsadf",
    "assessmentcriteria": "asdfadsf",
    "scenario": "asdfasdfasf",
    "tasknumber": 1,
    "taskarea": [
      "adfafads"
    ],
    "librarylinks": [
      "afdasfasdf"
    ],
    "studylinks": [
      "asdfasafs"
    ],
    "videoslinks": [
      "afdaafd",
      "asfdasfasf"
    ]
  }
}
`
    • 超文本标记语言**

下面是我的html代码

`<button
  class="btn rounded-pill btn-primary remove_module remove_module_button"
  data-id="5"
  data-modulename="module_1"
  type="button"
>
  Remove Module <i class="bx bx-x-circle ml-1"></i>
</button>`
    • jquery**

下面是我jquery代码

`$(document).on('click', '.remove_module', function() {
    var id = $(this).data('id');
    var modulename = $(this).data('modulename');
    var ok = confirm("Are you sure, you want to delete this Module?");
    if (ok) {
        $.ajax({
            method: "GET",
            url: "deletemodule",
            data: {
                id: id,
                modulename: modulename
            },
            success: function(data) {
                if (data == 1) {
                    alert("Course Deleted Successfully!");
                    window.location.reload();
                }
            }
        });
    }
});`
    • php**

这是我的php代码

`public function deleteModule(Request $request){
        $id =  $request->id;
        $modulename =  $request->modulename;
        $data = Course::where('id', $id)->get();
        $modueldata = $data[0]->modules; //get module data in modules column

     }`
    • 我需要删除用户给定的对象并按顺序重命名所有模块。**
`{
    "module_1": {  
        "modulename": "asasdasd",
        "moduletypes": "optional",
        "moduleid": "23443",
        "moduleaim": "asdasgdfg",
        "learningobjectives": "dgdg",
        "assessmentcriteria": "dfgdg",
        "scenario": "dgdgdg",
        "tasknumber": 1,
        "taskarea": [
            "dgdfgdfg"
        ],
        "librarylinks": [
            "dfgdfgdfg"
        ],
        "studylinks": [
            "dgdfgdfg"
        ],
        "videoslinks": [
            "dgdfgdfg",
            "sdfasdfasf"
        ]
    },
    "module_2": { 
        "modulename": "science",
        "moduletypes": "optional",
        "moduleid": "3234",
        "moduleaim": "sadfsdfgsdaf",
        "learningobjectives": "asfdsadf",
        "assessmentcriteria": "asdfadsf",
        "scenario": "asdfasdfasf",
        "tasknumber": 1,
        "taskarea": [
            "adfafads"
        ],
        "librarylinks": [
            "afdasfasdf"
        ],
        "studylinks": [
            "asdfasafs"
        ],
        "videoslinks": [
            "afdaafd",
            "asfdasfasf"
        ]
    }
}`

这就是我需要做的。

gudnpqoy

gudnpqoy1#

它只是数组处理和未设置。

$modules        = $data[0]->modules; //this is array if not convert it too
$moduleToRemove = $request->modulename;
$filteredArray  = array_filter($modules, fn($module) => !($module === $moduleToRemove), ARRAY_FILTER_USE_KEY);
$finalModule    = [];
$i = 1;
foreach ($filteredArray as $moduleData) {
    $finalModule['module_'.$i] = $moduleData;
    $i++;
}

相关问题