ts

Injecting a Relationship

Posted 8 months ago by ts

Hi all,

So in a controller I'm accessing the establishment property of the authenticated user, which returns an Eloquent belongsTo relationship.

I'm accessing it in several places, currently like so:

Auth::user()->establishment->...

Which I feel is bad. I'd really like to inject the establishment into the controller __construct method so I can access it as $this->establishment, but when I do, it returns a new instance of the establishment, not the user's establishment.

I've tried declaring a singleton in a service container and setting it to Auth::user()->establishment, but Auth::check() is returning false because (I'm guessing) the user hasn't been resolved?

I feel like I'm close to a solution. If there are any Laracasts or articles that might shed some light, that'd be ace.

Cheers, Tom

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.