samedi 9 février 2019

Fetch field from related model in Laravel?

I have ResultTest model with the following relation:

public function doctor()
{
    return $this->hasOne('App\Doctor', 'user_id', 'doctor_id');
}

Model App\Doctor has also relation:

public function distributor() {
  return $this->belongsToMany(DistributorContacts::class);
}

So, when I fetch data from model ResultTest:

It is becomes complicated and ugly fetch specific field from related model:

dd($conclusion['test']->doctor()->first()->distributor->first()->name);

And sometimes there is not relation ->doctor()->first() and it fails



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire