Laravel 5.2 pluck() multiple attributes from Eloquent Model Collection
Laravel: 5.7
if method is returning relation
use get()
e.g
$lesson = Lesson::find(1);
$lesson->users()->get(['id', 'name']);
on collections
use only()
$user = collect(['id' => 1, 'name' => 'Foo', 'role' => 'A']);
$user->only(['id', 'name']);
multiple arrays
$users = collect([
['id' => 1, 'name' => 'Foo', 'role' => 'A'],
['id' => 2, 'name' => 'Bar', 'role' => 'B'];
]);
$result = $users->map(function($user){
return Arr::only($user, ['id', 'name']);
});
var_dump($result);
I know this isn't the most recent question, but in case someone stumbles onto it from google later, see the Only Collection Method
$lesson->users()->only("id", "name")->toArray();
should be what you're after.