Level 73
Jul 17, 2018
2
Level 2
Update different rows with different values in one query!
Update different rows with different values in one query!
Some example:
$commentsOderdListData = [
['id'=>1,'sortOrder'=> 0],
['id'=>2,'sortOrder'=> 2],
['id'=>3,'sortOrder'=> 1],
['id'=>4,'sortOrder'=> 3]
];
I know maybe something like this will work:
$postID = 2;
$post = $this->model->where('id', $postID )->firstOrFail();
foreach($commentsOderdListData as $data){
$post->comments()->where('id'=>$data['id'])->update(['sortOrder'=> $data['sortOrder']]);
}
- Is there a way to just update this data in one query, for best performance ?
Please or to participate in this conversation.