Kaustubh
2 years ago

Update Data using array

Posted 2 years ago by Kaustubh

I want to update multiple rows in Permission Table. I have permission id in array but i am unable to use where clause in query.

I want to use permissionid in where clause and the permission id is in array of data.

I can insert new records using array but i am finding difficulty to update using array.

Here is my controller

public function UpdatePermission(Request $req)
{
    $input = $req->all();
    $i = 0;
    $count = count($input['roleid']);
    while($i < $count){

        $data[] = array(
            'permissionid' => $input['perid'][$i],
            'per_roleid' => $input['roleid'][$i],
            'per_pageid' => $input['pageid'][$i],
            'view' => $input['view'][$i],
            'add' => $input['add'][$i],
            'edit' => $input['update'][$i],
            'delete' => $input['delete'][$i],
        );
        $i++;
    }

    // dd($data);
    
    DB::table('permission')->whereIn('per_id',$data['permissionid'])->update($data);
    return redirect()->back();
}

Here is my $data[] array

array:3 [▼
  0 => array:7 [▼
    "permissionid" => "6"-------------------> I want to use Permission Id in where clause
    "per_roleid" => "4"
    "per_pageid" => "1"
    "view" => "on"
    "add" => "on"
    "edit" => "on"
    "delete" => "off"
  ]
  1 => array:7 [▼
    "permissionid" => "7"-------------------> I want to use Permission Id in where clause
    "per_roleid" => "4"
    "per_pageid" => "2"
    "view" => "on"
    "add" => "off"
    "edit" => "off"
    "delete" => "off"
  ]
  2 => array:7 [▶]
]

Please sign in or create an account to participate in this conversation.