Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ictoplossing's avatar

How to get (pluck) name from a related table?

Hello everyone,

I have a model Person which return:

{
"id": 241,
"forename": "Johan",
"label": "Johan / Dutch",
"value": 241,
	"person_addresses": [
		{
			"id": 111,
			"name": "Stad",
			"dictionary_geos_id": 328,
		}
}

And I have a table "dictionary_geos" with two columns: ID and postal_code

How to get data from "postal_code" by "dictionary_geos_id"?

Tnx

0 likes
10 replies
JabatoForever's avatar

hi @ictoplossing you need to add a relation between the Adress and the dictionaryGeo table

public function dictionaryGeo() {
    return $this->belongsTo(DictionaryGeo::class);
}

then when u get the relation add the nested one

Person::with('addresses.dictionaryGeo')->get();

Now inside each person address u will have the related dictionaryGeos model.

1 like
JabatoForever's avatar

@ictoplossing yes sorry, i didn't noticed your column does not follow Laravel conventions for relations so u should specify the foreign key name when defining the relation and should work

public function dictionaryGeo() {
    return $this->belongsTo(DictionaryGeo::class, 'dictionary_geos_id');
}

or as an alternative change the column name to be the singular of the table name and follow Laravel conventions so it would be dictionary_geo_id

ictoplossing's avatar

Hi @JabatoForever , Now i get an error:

Call to undefined relationship [addresses] on model [App\Models\Person].

Model Person.php:

public function dictionaryGeo()
{
	return $this->belongsTo(DictionaryGeo::class, 'dictionary_geos_id');
}

PersonController.php:

$persons = Person::latest();

$persons = $persons
	->with([
		'personAddresses',
		'spokenLanguages',
		'createdByUser',
		'addresses.dictionaryGeo'
	])
	->get(['id', 'forename', 'name']);

If I use 'dictionaryGeo' (without 'addresses') I get dictionary_geo: null

Why?

DhPandya's avatar

@ictoplossing Error message is clear that you don't have any relation named addresses on your Person model.

ictoplossing's avatar

Hi @DhPandya ,

I have this relation:

public function personAddresses()
    {
        return $this->belongsToMany(
            Address::class,
            'person_addresses'
        );
    }

This code return:

{
"id": 241,
"forename": "Johan",
"label": "Johan / Dutch",
"value": 241,
	"person_addresses": [
		{
			"id": 111,
			"name": "Stad",
			"dictionary_geos_id": 328,
		}
}

I need to get from a table 'dictionary_geos' postal code (column 'postcode') of the city by ID.

How to get it?

DhPandya's avatar

@ictoplossing Simply add your dictionaryGeo relation.

$persons = Person::latest();

$persons = $persons
	->with([
		'personAddresses',
		'spokenLanguages',
		'createdByUser',
		'dictionaryGeo'
	])
	->get(['id', 'forename', 'name']);
ictoplossing's avatar

@DhPandya , Yes, I did it:

"dictionary_geo": null

I would like to get here postal code from the table 'dictionary_geos'

johndivam's avatar
$persons = Person::with('person_addresses')->get();

$names = collect($persons['person_addresses'])->pluck('name')->first();

dd($names);

Please or to participate in this conversation.