mercredi 4 avril 2018

Laravel 5 eloquent - add child field to parent model

I have a user model which has a child relationship called teacher. How can I add a field from the related teacher model to the dataset returned of the parent user model?

I would like the user model returned to have a structure like below:

user.firstname

user.lastname

user.teacher.address

I have tried using the following and variations thereof with no success:

            $query->select('firstname', 'lastname')
            ->addSelect( \DB::raw('teachers.address AS address') );

Thanks in advance,

K...



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire