Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Edgy's avatar
Level 1

Auth User in Query, is this also good practise?

Hi guys, I have a quick question. is this a good practise?

$test = WorkersTest::where('workers_id', Auth::user()->id)->get();

basically this is a query where is only select based on their login auth::user is this a good practise?

Thank you

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

Typically, I would use a relationship in similar cases:

auth()->user()->workerTests;

But it amounts to the same thing really. The route(s) that use this Controller method would be protected by auth middleware of course.

Also, you can get the id for the authenticated user using Auth::id() or auth()->id() which looks a little cleaner to my eye.

1 like

Please or to participate in this conversation.