Sunday, April 8, 2018

Laravel use different function name on model

Users and articles are one to many relationship database.

if I use owner instead on user in article model, we should add the foreign key such as:

class Article extent model
{
  public owner() {
    return $this->belongsTo('User', 'user_id');
  }
}

in the controller or view we can use
$article->owner->name;




No comments: