Home > Enterprise >  laravel prevent overload data on Auth::user()
laravel prevent overload data on Auth::user()

Time:04-12

My model User have some relation: profile, activites, etc... on my controller i have this:

$user = Auth::user();
$user->load(['profile', 'activities']);

excpected result on my blade is that my variable $user is with my relationships and the Auth::user() data remain the users table data only;

i have a layout layout.blade.php and a view index.blade.php that extends my layout;

in my index i want to use the relation data and in my layout i want only the users data; but i always have the relation loaded from controller

CodePudding user response:

If you load any relationship, laravel will add that relationship in your user object automatically.

If you don't want this and want the user only, you can duplicate the object first to another variable before loading any relation.

Second option, you can get the user again

CodePudding user response:

If you want to have the same object but without the relationship you have multiple choices:

$user->unsetRelation('profile');//unset specific relationship on the $user Object
$user->unsetRelations();//unset all relationships on the $user Object
$userWithoutRelationships = $user->withoutRelations();//clone the current Object but without relationships
  • Related