Support the ongoing development of Laravel.io →
Requests Database Eloquent
Last updated 2 years ago.
0
Solution

There are two ways you can go with this. First is to loop through all the elements and renaming it. (maybe using the dot style accessors)

Second is you can have mutators in your model. Example : To fetch photographer_users.username as 'username' :

public function getPhotographerAttribute()
{
    return $this->photographer()->username;
}
Last updated 2 years ago.
0

Thanks for your help, With the mutator, at which step should I call the getPhotographerAttribute, or will this be "automatically" always renamed? Sorry I haven't used yet the accessors and mutators so I am not very familiar with their use.

Regarding the first option of looping, I was keeping that as a backup if I couldn't find anything else... feels super long to do vs using the premaid collection

Last updated 2 years ago.
0

They are automatically called when you access that attribute.

Last updated 2 years ago.
0

Thanks, I ended up using the array solution that way I could add extra external datas I needed, worked out perfectly! :-)

Last updated 2 years ago.
0

Hi commandantp

Would you mind sharing your idea, i still try to have an alias array to rename the db column name.

if there are much more records,will it have performace issue ?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.