Support the ongoing development of Laravel.io →
Authentication Eloquent Views
Last updated 2 years ago.
0

As for sent messages, I would either define a one-to-many relation for sent messages and users, or pass a user to the sent() scope as an argument. Either way works for getting Auth::id() out from the model.

0

There are quite a few things to cover here.

Auth::user() is the correct way to get the authenticated user.
If you have a lot of User settings, have a look at Laracasts videos:
https://laracasts.com/lessons/managing-mass-user-settings
and
https://laracasts.com/lessons/managing-mass-user-settings-part-2
They cover a great way to store large user preferences as JSON in a single column, then extract that on framework boot and register it as a singleton.

In a heavily Auth::user() dependant class/controller, I will inject it in the __construct, so I am referencing $this->user->name, instead of Auth::user()->name


As for sharing it across views, have a look at View Composers.
What I do is have a ViewComposer for all views that shares a variable as CurrentUser, that I can then access anywhere in any of my views.
This could apply to your e_unread also

ViewComposers are great for partials as well, where you may have a Select box that requires you to select a relatable item. Set up a ViewComposer to share RelatableItems with only that view, then you can include that partial wherever you need to, without worrying about populating it's RelatableItems.

0

Thanks for your incredible advices Xum and Towerful!

Towerful, at first I tried with the ViewComposer approach trying to pass a composer to my master layout but found that the variables cannot be accessed on templates that are extending the master layout or included views (a pity I think). I have two partials I need to pass e_unread and the user data, is this the correct approach then?

View::composer(['partials.header', 'partials.sidebar'], 'App\Http\ViewComposers\MyViewComposer');

In my case I need to pass the user variable to all the backends views so I put all my backend views in a backend folder and did this

View::composer('backend.*', 'App\Http\ViewComposers\BackendComposer');

This is the only solution I can think of apart from doing it on the BackendController.

Giancarlo.

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

topitox topitox Joined 25 May 2015

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.

© 2024 Laravel.io - All rights reserved.