0

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

2
  • Because you load the relations. But why not access the user values you need in layout and relations values in index? Commented Apr 12, 2022 at 10:08
  • 1
    The question is "why do you want to unload the relations", once it is loaded it's better to keep it there as you might need it and it will save you a DB query. Commented Apr 12, 2022 at 10:30

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.