Summer Sale! All accounts are 50% off this week.

vincent15000's avatar

Factory and model events (static::creating)

Hello,

In the boot method of the model, I have added a listener to the creating event in order to add automatically the owner id (authenticated user id) to the model.

When I want to fill the database with the factory, it doesn't work because there is no authenticated user. So the only way is to set a random user id among the users in the database.

But the code will all the way try to retrieve the authenticated user id while executing the event listener. How is it possible to use the factory without having to disable the event listener ?

Thanks for your help.

Vincent

0 likes
3 replies
bobbybouwmann's avatar
Level 88

I think you have two options here.

  1. In the creating event check if there is an authenticated user. If there is none you don't perform the action
  2. Always make sure you have an authenticated user as you suggested. This doesn't make much sense for certain test cases for example.
1 like
vincent15000's avatar

@bobbybouwmann Ok ... but if I don't perform the action, I don't have any owner id and the owner id is required to save a new model.

Is it possible to simulate a connected user to run a test ?

Please or to participate in this conversation.