jesse_orange_newable's avatar

Using auth()->user() within a test

In an application I'm building a Comment can have a relation to both a User and an Article, so when creating the comment I just do the following:


$article->comments()->create($request->validated());

Then to associate the User I hook into the creating event of the Comment model to associate the user with the comment like so:


$comment->user()->associate(auth()->user());

However, in my test case it always squawks about comment user_id being null, which leads me to believe that in the test case auth()->user() is null.

How can this be so when I'm using actingAs() when calling the route to add the comment?

0 likes
1 reply

Please or to participate in this conversation.