0

I want to display current user loggedin information but without relation. something I just need specific information about the user model.

I have tried this similar question Disabled eager relations

I try above answer and end up with empty user

window.Goyong =
      <?php echo json_encode([
          'csrfToken' => csrf_token(),
          'user' => $currUser->setEagerLoads([])
]); ?>

The result:

{
    "csrfToken": "kDc5WmebrWlHzyV77pdYZCW69kaiGK6ZC29zAvFn",
    "user": {}
}

How can I show current user without bringing any relation?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Aslam H
  • 1,669
  • 4
  • 21
  • 46

1 Answers1

0

You have two issues here:

  1. $currUser seems to be an empty User object (try with Auth::user() as suggested by @Paul instead)
  2. setEagerLoads([]) doesn't make much sense once the model has already been retrieved from the database. You can, however, remove relationship using setRelations([]).

Example:

$withEagerLoadedRelationships = Subscriber::query()->with('profiles')->first();
$withoutRelationships = $withEagerLoadedRelationships->setRelations([]);

Btw, unless you need those eager loaded relationships somewhere else you should act at the instantiation of $currUser removing the eager loading from the beginning.

Alessandro Benoit
  • 903
  • 10
  • 19