Support the ongoing development of Laravel.io →
Database Eloquent

I would like to prevent some users from accessing certain database columns based on role management. I can use setAppends() to change a models appends for accessors/mutators. However, I haven't found a way to change this on the fly when using eager loading.

The following example will enable accessors/mutators for the User model. How do I also set the appends for the Accounts model which I'm eager loading? Is there a better way to go about this?

$data = User::with('accounts')->get();
$data->each(function($item){
	$item->setAppends(array('accountId', 'createdAt', 'username', 'firstName', 'lastName'));	
});
return $data->toArray();
Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

narffy narffy Joined 20 Mar 2014

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.

© 2025 Laravel.io - All rights reserved.