List toolbar - update slugs

Hi! How do I properly update the slugs of all selected posts in a list?

When I use the code below, I get an error
"count(): Argument #1 ($value) must be of type Countable|array, string given"

public function onUpdateSlug() {
    if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) {
        foreach ($checkedIds as $modelId) {
            $data = News::whereIn('id', $modelId)->first();
            $slug = $data->id .'-'.str_slug($data->name);
            News::whereIn('id',$modelId)->update(['slug' => $slug]);
        }
    }
    return $this->listRefresh();
}

If I remove “foreach”, the code will work but all checked news will have the same slug.

Try

News::where('id',$modelId)->update(['slug' => $slug]);
1 Like

@daft unfortunately didn’t help, got the same error.

public function onUpdateSlug() {
    if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) {
        foreach ($checkedIds as $modelId) {
            $data = News::whereIn('id',$modelId)->first();
            $slug = $data->id .'-'.str_slug($data->name);
            News::where('id',$modelId)->update(['slug' => $slug]);
        }
    }
    return $this->listRefresh();
}

There’s another one 2 lines above, change whereIn to where

1 Like

@daft sorry, I missed that one. Yep, it helps, thank you! :slight_smile:

1 Like