samedi 26 août 2017

Return the model I belongTo based on foreign key

How can I get the full model based on the foreign key in a table using Laravel?

A user row will have a single tier it's associated with, represented by the ID as the foreign key.

I need to return the user with the tier it belongs to, and the entire contents of that tier as well.

In my User model, I've tried protected $with = ['tier'];, but this doesn't work.

User:

public function tier()
{
    return $this->belongsTo('App\Tier');
}

Tier:

public function users()
{
    return $this->hasMany('App\User');
}

User's table:

enter image description here

User's table structure fk index:

enter image description here



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire