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

khanvuthy's avatar

One to many relationship seem wrong

I have two tables, User:{id,name....} Resume:{id,user_id,....}

My User model :


/**
     * Get the resumes for the user.
     */
    public function resumes()
    {
       return $this->hasMany(Resume::class);
    }

My Resume model :


**
     * Get resume owner
     *
     * @return resume owner
     *
    */
    public function user(){
        return $this->belongsTo(User::class);
    }

When i try to get resume owner


>>> $rs->user->all()
=> Illuminate\Database\Eloquent\Collection {#767
     all: [
       App\User {#768
         id: 1,
         name: "Ed Kuhlman",
         email: "nyah01@example.com",
         created_at: "2017-05-28 14:04:20",
         updated_at: "2017-05-28 14:04:20",
       },
       App\User {#769
         id: 2,
         name: "Javonte Powlowski",
         email: "[email protected]",
         created_at: "2017-05-28 14:04:20",
         updated_at: "2017-05-28 14:04:20",
       },
       App\User {#770
         id: 3,
         name: "Aaron Keeling",
         email: "felicita97@example.net",
         created_at: "2017-05-28 14:04:20",
         updated_at: "2017-05-28 14:04:20",
       },
     ],
   }

What's the problem ?

0 likes
13 replies
bazie's avatar

try:

     * Get the resumes for the user.
     */
    public function resume()
    {
       return $this->hasMany('App\Resume');
    }

and

**
     * Get resume owner
     *
     * @return resume owner
     *
    */
    public function user(){
        return $this->belongsTo('App\User');
    }
EventFellows's avatar

What are you expecting? You defined that there is only 1 user that the resume belongs to... Why are you calling ->all()?

You are basically calling all() on the User Model - so you get all users.

3 likes
EventFellows's avatar

You can convert it to a collection with this:

collect([$rs->user]);

But as your query will always just be 1 result (you defined it like that on your relationship) laravel returns the model instance (and not a collection with one result)

But the real question would be: Why would you want a collection and not the model instance that you get with just calling $rs->user...?

2 likes
bazie's avatar

if you want to get resume owner, you can do it

$rs=App\Resume::find(1)

and in the views


foreach ($rs as $rs)
 {!! Form::label('name', 'Name:') !!}
    <p>{!! $rs->user->name !!}</p>
 

for example if you want to get name

EventFellows's avatar

@bazie your foreach will not work as $rs is not a collection. so you cannot loop through it.

1 like
bazie's avatar

in this case you can use compact('$rs') in your controller when you want to return views and then you will have

{!! Form::label('name', 'Name:') !!}
    <p>{!! $rs->user->name !!}</p>
sujancse's avatar
sujancse
Best Answer
Level 10

Hey @khanvuthy just try this

$rs->user;

This will give you the user related to Resume.

1 like

Please or to participate in this conversation.