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

FREDERIC LD's avatar

one to may relationship query

hi,

I am a bit struggling with this relation. not sure why....

Admin model. An admin has an employer

public function employer()
    {
        return $this->belongsTo(Employer::class);
    }

employer model. An employer has many admins

public function admins()
    {
        return $this->hasMany(Admin::class);
    }

The following query returns the relationship correctly but with too much data

$items = Admin::select('id', 'first_name', 'last_name', 'uuid', 'email', 'employer_id')->with('employer')->get();

I tried to trim the data to only get the employer name

$items = Admin::select('id', 'first_name', 'last_name', 'uuid', 'email', 'employer_id')->with('employer:name')->get();

but laravel returns null for the relationship

0 likes
1 reply

Please or to participate in this conversation.