Database factories without using HasFactory trait

PHOTO EMBED

Wed Mar 24 2021 17:00:56 GMT+0000 (Coordinated Universal Time)

Saved by [deleted user] #laravel #factory

NotificationFactory::new()->create();



use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Support\Str;

class NotificationFactory extends Factory
{
    protected $model = DatabaseNotification::class;

    public function definition()
    {
        return [
            'id' => Str::uuid()->toString(),
            'type' => 'App\Notifications\ThreadSubscription',
            'notifiable_id' => function() {
                return auth()->id() ?? User::factory()->create();
            },
            'notifiable_type' => 'App\Models\User',
            'data' => ['message' => 'text']
        ];
    }
}
content_copyCOPY