Add extra string in Laravel from search query result by pluck
One way would be to use Collections#map:
$outputArray = $list
->pluck('title', 'id')
->map(function ($title, $id) use ($books_selected) {
return [$title, in_array($id, $books_selected)];
});
Or in a shorter form if you're using PHP 7.4:
$outputArray = $list
->pluck('title', 'id')
->map(fn($title, $id) => [$title, in_array($id, $books_selected)]);
Your final array should be in this form:
[
id1 => ['title1', true],
id2 => ['title2', false],
...
]
source: StackOverflow