Software contractor at Floor9design Ltd

Member Since 5 Years Ago


Experience Points

4,880 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
Best Reply Awards
Best Reply
  • start your engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-in-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber Created with Sketch.


    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.


    Earned if you have a lifetime subscription to Laracasts.

  • evangelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 1
120 XP
1 month ago
Activity icon

Replied to User In Laravel Controllers

I'd not thought of that.. that's a nice idea. My (limited understanding) is that some Event hooks are triggered after Auth facade, so I can call it then and populate variables.

Let me see if I can get it to work!

Thanks for the idea.

Activity icon

Replied to User In Laravel Controllers

No, you're not understanding.

Calling auth()->user() twice is not the issue. It is the children that causes the issue.

As an example: foreach(auth()->user()->children as $child) or worse the children of children: and variants of it is the issue. This could be a huge optimisation and could be enforced at a top level.

To be clear... this is why I am saying N+1. When parsing over a child you hit the database separately with every child element. It is not optimised unless you use $user::with('child')->find($id) or some similar lookup.

However, this is sidestepping the syntactic niceness of always having predefined these items. What if I ALWAYS use 20 variables. Are you suggesting I redefine them in every action?

Activity icon

Replied to User In Laravel Controllers

Ok, thanks all for the help. Just to be clear:

  • auth()->user()
  • auth()->user()->relationshipA
  • auth()->user()->relationshipB

This is 2+ lookups, with no top limit. If foreach() is badly used within a sub controller (and foreach is common in some of the lookups), then this causes 2 + count(foreach()) lookups.

This is the N+1 problem for loading children.

  • $id = auth()->user()->id
  • $user = User::with('A', 'B', 'C')->find($id)

This is only 2 lookups (internally 3) max.

The second approach prevents foreach loops causing huge amounts of separate DB calls. Eager loading to prevent the N+1 is suboptimal where this is unlikely. It is optimal where guaranteed or very likely.

To be clear: If I set either response to be an internal variable ($logged_in_user) across all controllers (and views etc), then I have globally set variable that does not cause lazy load issues.

Also, syntactically, I can set $variable_of_interest1, $variable_of_interest2, $variable_of_interest3 in the Controller constructor, which saves me duplicating the code in each child controller.

Thus, the following easy to read code can be done:


class SomethingController extends Controller
public function something(): View
    $something = Something::paginate($this->paginate_default);

    return view(
             'something' => $something,
             'logged_in_user' => $this->logged_in_user
             'user_property' => $this->user_property 


With the above, paginate_default, logged_in_user and user_property are "just available in any controller" and the logic is centralised in the Controller. (note: paginate_default could be a user property, or a child property or a number.. it can be calculated or just a number) The Controller constructor can be as complicated as is required, and it's not duplicated or possible to mess up.

I also know that you can "share logged in user" to views easily... I am simply illustrating a use case. If paginate_default relies on some user calculation, the above is still valid. It needs to be loaded at the Controller level.

It sounds like it can't be done in this way. If anyone knows different, let me know, but thanks for your help all.

1 month ago
Activity icon

Replied to User In Laravel Controllers

PS: I know I could write these functions into the User model:

  • auth()->user()->setUpAllSorts()

.. but this starts to make the User model have a lot of responsibilities and the point of MVC is that

  • models know about themselves and have no/limited outside responsibilities
  • controllers control the flow and loading of data

This is a classic example of loading and controlling flow. It's a controller responsibility.

Activity icon

Replied to User In Laravel Controllers

  • $logged_in_user = auth()->user()
  • $logged_in_user->eagerLoadSomeData()
  • $some_other_var = ┬Čauth()->user()->eagerLoadSomeOtherData()
  • $even_more_data_var = ┬Čauth()->user()->eagerLoadEvenMoreData()

This is happening on 90% of controllers. These would be easy to miss-implement, and even if they are correctly implemented, it's just a bad code-smell to be duplicating this code in most controllers.

To be clear:

  • auth()->user()->someSpecificchild() will be called in some places
  • auth()->user()->someOtherSpecificChild() will be called in some places
  • auth()->user()-> ... some multi level lookup will be checked in others.

I'm attempting to do this at a top level to centralise this functionality in a single point. These variables should be at a global level, for example, the Controller constructor, that many things can easily reference.

Either way... back to the question. Is there a way to load the auth()->user() semi globally and interact with it and related variables at a global level. As i mentioned, doing this is the Controller seems to fail.

1 month ago
Activity icon

Replied to User In Laravel Controllers

I mean.. That's fine and useful, but I still need to make controller based decisions all over the place based on the user and the user child objects.

I want to be able to access $this->logged_in_user in ANY controller because there are other things that need to be reviewed and looked at as part of controller decision making.

I sense that people are avoiding this because it's difficult... Does anyone know the answer to ONLY that part of the question?

Thanks all!

Activity icon

Replied to User In Laravel Controllers

That's not the thing I'm asking. although it is useful to know!

Note: I don't want to force all users to eager load relationship x, i just want the logged in user to have eager loaded relationship x.

$logged_in_user => already has x

$users = Users::all() => I'll only load x if I need it on that call.

However: I'm more interested in knowing how to load $logged_in_user for all controllers in one location: at that point I can do the optimisation.

Activity icon

Started a new Conversation User In Laravel Controllers

I have inherited a system in laravel that uses over 40 controllers and probably 200 views.

I am attempting to clean up the code and use best practice. Given that certain calls are made pretty much everywhere, it makes sense to define it somewhere "semi globally". I would assume this would be in the Controller from which all controllers extend.

One object is $user, and has child $user->organisations and $user->organisation->locations.

Loading this at a base controller (or equivalent) way would also give me the advantage that I could ensure child relationships were eager loaded in an optimal way ensuring any foreach style code never results in multiple small database lookups. There are a number of other items I want to do this for with similar ramifications for database optimisation. These all use the Auth::user(), and they affect permissions with child objects.

Given that there are about 20 properties/variables of use to be shared (all dependant on Auth::user()) removing this duplicated code from almost every method is a huge improvement.

My aim is to be able to reference the logged in user from any controller, and already have pre-loaded all the child/related objects.

The problem? Auth::user() cannot be run inside the Controller constructor.

How do I load the current user globally (and use it to eager load some relationships) across all controllers.

I would like to use $this->logged_in_user inside any controller.