Owls Department

Owls Department- A Digital Studio

2 min read
  • laravel
  • php

Laravel Tips: Using pluck() method on nested Eloquent relationships.

Laravel Tips: Using pluck() method on nested Eloquent relationships.

Normally when querying a given model with its relationships, Laravel allows you to use dot notation to reach nested ones.

Relationship definitions

For example imagine we have 3 models - Configuration, EngineType and CarBrand. For the sake of simplicity the linkage goes as follows:

  • Configuration::class belongs to an EngineType::class
  • EngineType::class belongs to a CarBrand::class

Within Configuration.php model:

public function engineType(): BelongsTo  
{  
    return $this->belongsTo(EngineType::class, 'engine_type_id', 'id');  
}

Within EngineType.php model:

public function carBrand(): BelongsTo  
{  
    return $this->belongsTo(CarBrand::class, 'car_brand_id', 'id');  
}

Finally leave CarBrand.php model with no relationship (even inverse ones).

Fetching and obtaining nested attribute

Now imagine you fetch all of the configurations and you want to know how many unique car brands were assigned to all of these configurations.

Define an array with relationship that represents the nested structure. Keep in mind that the structure of our database is fully normalised to avoid any unnecessary redundancy. In other words CarBrand is not directly connected with Configuration.

$relationships = [
    'engineType.carBrand',
];

Pulling everything together can be done like:

$configurations = Configuration::with($relationships)->get();

Now lets use the pluck() method to extract primary keys of all fetched car brands.

$carBrandIds = $configurations->pluck('engineType.carBrand.id')->unique();

This is it. We specify the column name under relationship we're interested in. In this particular case we want ID of a CarBrand. The pluck() will return a collection. As multiple configurations can have the same CarBrand (through EngineType) we call unique() to remove duplicated IDs (it works as distinct on the SQL query statement level).

Related Content

03

Let us turn your
vision into reality

Kuba Sarata - Get in touch

GET IN TOUCH

Whatever your ideas are we would love to help take it to the next level.

[email protected]
+48 530 330 857

Schedule a meeting →