mercredi 3 juillet 2019

in one to many relationship, how to define foreign key and local keys

User has many notes. Notes belong to user.

in User Model:

public function notes()
{
    return $this->hasMany(User::class,'user_id','user_id');
}

in Note Model:

public function user()
{
    return $this->belongsTo(Note::class,'user_id','written_by');
}

Now, user has user_id as a primary key inNote model, written_by is used to connect with the user. What should I fill in foreign key and local keys? In many blogs, they explained wrongly and when I tried several times, it didn't work and I came here to get some help.

Thank you



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2KWPsl4
via IFTTT

Aucun commentaire:

Enregistrer un commentaire