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

Ligonsker's avatar

Setting auth details as controller's properties

Hello,

I've been using auth()->user()->username on many lines and noticed that when I want to do some tests I have to manually change all occurrences which is cumbersome.

(There are more properties which I sometimes need to change in multiple places but for this example I only use username)

For example:

class SomeController extends Controller
{
    public username = auth()->user()->username;
   
    ...
}

Would that be a good practice to set the auth() details as the controller's property? This way I will only need to change it in one place if I ever need to test something.

Any downsides to that? Or is there a better way to do it?

Thanks

0 likes
8 replies
Snapey's avatar

you should never need to change code to accommodate tests.

Why are you not using actingAs helper?

2 likes
renato.hysa.dev's avatar

As another user said, you should be using actingAs or Auth::shouldReceive() depending on your use case.

In my tests for example, I use Auth::shouldReceive() because I'm using DDD in the project and the tests are not tied to Eloquent, but in your case, you should be using actingAs.

1 like
martinbean's avatar

@ligonsker I don’t think you’re giving us the full picture here, since the example you give just isn’t possible in PHP. You can’t use expressions like that to set property values in classes.

To be honest, usages of helper functions like auth to me are just a code smell as there’s usually a better way to access things, usually via dependency injection.

So, present an actual problem you’re having and the actual code, and then we’ll be better able to answer with better ways to do whatever it is you‘re trying to do.

1 like
Ligonsker's avatar

@snapey, @renato.hysa.dev I never saw that before so I will look into it, which might be the solution. Although sometimes I need to do special "testing" in prod in "emergency" 😅

@martinbean, my bad I meant to use the __construct() method:

class SomeController extends Controller
{
    public username;

    public function __construct() {
        $this->username = auth()->user()->username;
    }
   
 }   
    ...
}
martinbean's avatar

@Ligonsker That wouldn’t work either, as the session (so therefore, in turn, the authenticated user) has not been available in the constructor of controllers since Laravel 5.3 (https://laravel.com/docs/5.3/upgrade#5.3-session-in-constructors), which was released in August 2016.

Can you save us all a lot of time and just show us the actual code and actual problems you’re facing, instead of making up pseudo-code? I also fail to see why you would need to set authentication details as controller properties, when typically only one method in a controller class is invoked in a single request.

Merklin's avatar

In your user model:

    public function getUsernameAttribute(): string
    {
        return $this->name;
    }

If you have change the default field from name to username, then return $this->username or whatever you want.

This way, when you call auth()->user()->username or $user->username, they will return what is set in the above function.

In a project of mine I've set fields name and last_name, and using this:

    public function getFullNameAttribute(): string
    {
        return ucfirst($this->name) . ' ' . ucfirst($this->last_name);
    }

when I call auth()->user()->full_name it returns for example John Doe

Same can be used in a relation. If you have a model related to the User model, you can call $model->user->full_name (in your case, $model->user->username)

Merklin's avatar

@Snapey ... I have to manually change all occurrences which is cumbersome.

By setting up username as a mutator, he have to change it in only one place, right?

Please or to participate in this conversation.