One solution could be to pass the user ID as a parameter to the seeder and use it to associate the item with the user. This way, you can still apply the global scope for multitenancy while seeding the database. Here's an example:
In your seeder:
$user = User::factory()->create();
Item::factory()->forUser($user->id)->create();
In your Item model:
static::creating(function (Item $item) {
if (auth()->check()) {
$item->user()->associate(auth()->user());
} elseif ($item->user_id) {
$item->user()->associate(User::find($item->user_id));
}
});
public function scopeForUser($query, $userId)
{
return $query->where('user_id', $userId);
}
This way, if the user is authenticated, the item will be associated with the authenticated user. If not, it will be associated with the user ID passed to the forUser scope.