PaddaelsM
PaddaelsM

Reputation: 457

Laravel One to Many relation doesn't work as intended?

I have a big problem. I have a Model containing a foreign key on "another model".

My first Model is a user and my second model is project. A User can have many Projects and a Project belongs to exactly one User. Since I have this kind of relation, I do store the reference inside my project-Model inside the column user.

class Project extends Model {
    public function user() {
        return $this->belongsTo('App\User','user');
    }
}

According to the docs, I should be able to get the properties of my User-Model using $project->user->namebut when I do a var_dump on $project, I only get the user ID, I've stored inside my projects-table instead of an User-Object.

Upvotes: 0

Views: 72

Answers (1)

user223321
user223321

Reputation: 153

You have to mention

In user model

public function projects()
{   

    return $this->hasMany('App\projects);

}

Upvotes: 1

Related Questions