What is the best way to query a user’s data in Laravel?

Asked

Viewed 160 times

1

When presenting multiple user data in a view, it is more advisable to do it in what way?

Option 1 (pass data to view):

$usuario = Auth::user();
return view('painel.usuarios.perfil', compact('usuario'));

Option 2 (directly in the view):

Auth::user()->name
Auth::user()->email

When I talk about the best option I am considering the good practices and especially the performance issue. Consider the Laravel 5.3 framework

  • 2

    Option 1 is very strange, I was confused, could it improve

  • 2

    It was my mistake, I forgot to replace User::user() by Auth::user()

  • You need that one $usuario ( which is logged ), either in a view or in multiple views?

  • 1

    Only one view.

1 answer

2

This is a matter of opinion and need, but I usually like to return by the controller, not to leave too large attribute calls in the view:

$usuario = Auth::user();
return view('painel.usuarios.perfil', compact('usuario'));

And the data that is in another table but within the user (in this example), manipulo in the view, not to return many variables:

$usuario->cpf->numero;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.