Laravel Pluck Fields from Relations

Laravel pluck fields from relations

You can use Laravel's pluck method as:

$sellers = Seller::with('user')->get()->pluck('user.first_name', 'id')

How to pluck fields from a distant relationship in Laravel

but I want "images" to have only urls as strings like:

"images":[1.png,2.png,3.png .....]

what should I do?

The quickest solution would be to add the $visible property to your App\Images model, like so:

public $visible = ['url'];

This will remove all the fields from the json, including the pivot field, except for those defined in the visible property. This solution works fine but it still selects all columns from the images table, if you want it to select only the minimum required fields (so you improve performance) for the relation, you can do:

$stocks = Stocks::with('images:url', 'tags')->get(); // this is a shortcut for selecting only url

Please, be aware that the solution above works fine for ManyToMany (N:N) relationships. However, for a hasMany (1:N), you'd have to select the primary key and all related foreign keys as well.

Hope it helps.

References:

Eloquent: Relationships#Eager-Loading

UPDATE

It worked but how can I remove the key url: and store only values 1,2,3

->pluck() is the collection function to do that, however, you can't easily pluck fields from a distant relationship without discarding the rest, but this macro will do the trick.

->pluckDistant()

In your AppServiceProvider, under the boot method, add this macro function:

public function boot()
{
/**
* Shortcut to pluck a field in a relationship.
*
* @param string $relationship The relationship name
* @param string|array $value
* @param string|null $key
*/
collect()->macro('pluckDistant', function($relationship, $value, $key = null) {
return $this->map(function($item) use($relationship, $value, $key) {
$relation = $item->getRelation($relationship);

if (get_class($relation) == \Illuminate\Support\Collection::class ||
get_class($relation) == \Illuminate\Database\Eloquent\Collection::class) {
$item->setRelation($relationship, $relation->pluck($value, $key));
}

return $item;
});
});
}

And then, you do this:

$stocks = Stocks::with('images:url', 'tags')->get()->pluckDistant('images', 'url');

Laravel - How to pluck field from nested relations

Try this:

$user->groupUsers()
->with('group.promotions')
->get()
->pluck('group.promotions.*.const')
->collapse();

Laravel pluck with relations fails

Ok, I don't exactly know why, but I can't "refine" using Eloquent Query function pluck, but using Collection function lists:

$trucks = TttatTblTrucks::with('trucktype')->whereBetween('created_at',[$from, $to])->get()->lists('trucktype.description');

I guess it must be related with the fact that with is modifying the result and you longer can refine on a model, but on the returned collection.

By the way: lists is not deprecated on Collections. It's only that the function lists has been renamed to pluck on Eloquent Queries, but it remains as lists on Collections (https://laracasts.com/discuss/channels/laravel/lists-deprecated-replacement).

Laravel pluck an array from nested relationship

This gives you all roomnumber results in one collection:

$roomnumbers->pluck('floorroomcount')->collapse()->pluck('roomnumber')->collapse();


Related Topics



Leave a reply



Submit