ts

Experience

14,145

0 Best Reply Awards

  • Member Since 3 Years Ago
  • 221 Lessons Completed
  • 2 Favorites

18th April, 2018

ts left a reply on Injecting A Relationship • 1 week ago

@bobbybouwmann yeah, I've got my User model to use route model binding. I didn't think establishments would be necessary because a user belongsTo an establishment, so retrieving the user's establishment would be predictable. However, to futureproof (haha) the app, I'm going to change the relationship to belongsToMany - in future a user may be responsible for more than one establishment.

17th April, 2018

ts left a reply on Injecting A Relationship • 1 week ago

@bobbybouwmann that's excellent, thanks so much.

I think the "best" way in this instance would be to use route model binding. The routes don't currently include the establishment, so I'll rewrite them to do so.

Thanks again!

16th April, 2018

ts started a new conversation Injecting A Relationship • 1 week ago

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

15th April, 2018

ts started a new conversation Repository Pattern • 1 week ago

Hi all,

I just wanted to check I was on the right lines with this. It feels neat to me, but I've not used Laravel for a while and am just getting back into the swing of things.

I have an establishment that has visitors, that will be deleted after a certain time by a scheduled console command.

The console command is as so:

    public function handle()
    {
        Establishment::all()->each(function ($establishment) {
            $establishment->deleteExpiredVisitors();
        });
    }

The deleteExpiredVisitors() method on the Establishment class is as so:

    public function deleteExpiredVisitors()
    {
        $visitors = (new VisitorRepository)->getExpiredVisitorsForEstablishment($this);

        $visitors->each(function ($visitor) {
            $visitor->delete();
        });
    }

And the VisitorRepository is as so:

    public function getExpiredVisitorsForEstablishment(Establishment $establishment)
    {
        return $this->model->where([
            ['created_at', '>=', now()->subDays($establishment->days_retention)],
            ['establishment_id', '=', $establishment->id],
        ])->get();
    }

I suppose what I am asking is:

  • Is a repository necessary? From browsing the forum it seems they were popular a few years ago but I can't find much up-to-date reading on them.
  • How's my code.

Good to be back!

15th October, 2015

ts left a reply on Best Place To Store User Attributes • 2 years ago

Thanks for the input all. It seems obvious now I've slept! I think if I add a role column as an integer to the Users table, make a Roles table, and then use ACL to control how users can access content.

14th October, 2015

ts started a new conversation Best Place To Store User Attributes • 2 years ago

Hi all

We are in the process of creating a content management system for a school.

All faculty will have a log in, which is obviously stored in the users table.

My question is, where is the best place to put attributes that pertain to these staff members. For example, we have senior management, which might have access to actions or areas that other staff might not.

Obviously I could create an is_management column in the schema, but I'd imagine this to get unwieldy down the line as more attributes are added.

Advice is appreciated – are there any posts (or laracasts!) that might illuminate?

Tom

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.