简体   繁体   中英

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->name but 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.

You have to mention

In user model

public function projects()
{   

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM