mercredi 13 juin 2018

Call Eloquent collection functions

I have created a model relationship between 3 different tables/models.

Since I get a collection of objects due to hasMany-property I have to use a for-loop to access each of the Models methods in order to get the data I want. Is there anyway to tell that I want it to run the same function on all the objects?

Pseudo code:

Model A     //HasMany Model B
Model B     //HasMany Model C, Belongs to A
Model C     //BelongsTo C


$foo = new User::Find(Auth::id());

//Need to loop the collection of data in order to get the information
foreach($foo->permissions as $permission)
{
    $name = $permission->permissionsTypes->name;
}

I have tried to do this:

$$foo->permissions->permissionsTypes;

But since it is a collection it does not work.

Is there any other way to get this information without looping through the array?

Thanks for any guidance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire