mardi 25 octobre 2016

Laravel 5 - Return Full Object From Model Relationship Query

I have a model that looks like:

class User extends Model{

    public function projects(){

        $this->hasMany('Project', 'project_id')

   }

}

When I use the above method by calling $user->projects()->get() after initializing the class, I get a collection of only info related to the project(s):

Object [attributes:protected] => array(
     [project_name] => 'my project',
     [project_description] => 'my other project',
     [created_at] => '11-22-2015 04:23:12'
)

How do I get a collection that has data for both the project(s) and the user? I am looking for something like:

Object [attributes:protected] => array(
     [user_name] => 'macdadd',
     [user_type] => 4,
     [created_at] => '11-22-2015 04:23:12',
     [project] => array(
         array(
             [project_name] => 'my project',
             [project_description] => 'my other project',
             [created_at] => '11-22-2015 04:23:12'
         )
     )
)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire