-3

I am trying to create a sorting system (drag and drop ) . But the problem is it's getting array while it requires string. my code in the controller is :

public function sort(Request $request){
 
  $imageids = request()->input("imageids");

  $data = json_decode($imageids,true);
  
    if (is_array($data)) {
        foreach ($data as $value) {
            $taskItem = Task::find($value['id']);
            $taskItem->priority = $value['priority'];
            
            $taskItem->save();
        }
    } else {
        $taskItem = Task::find(request()->input("id"));
        $taskItem->priority = request()->input("priority");
         
        $taskItem->save();
    }
     
}
Shishir
  • 187
  • 1
  • 8
  • 1
    it may be worth moving your `is_array()` check to above the `json_decode` and then handling it in that fashion. Otherwise you'll need to change the way this page is "posted" to in order to post as a json string rather than as an array... – Joshua Mar 18 '21 at 22:29
  • still getting the same error – Shishir Mar 18 '21 at 22:47

1 Answers1

0

your request()->input("imageids") is returned array, so you don't need json_decode. Another way use condition like below:

if(is_string($imageids))
{
  $data = json_decode($imageids,true);
}
else
{
  $data = $imageids;

}
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73