skip to Main Content

Laravel – How to prevent unique error if I have 3 fields in a table/model but need more than 1 item? (factory)

I have a unique index on 3 fields: public function up(): void { Schema::create('items', function (Blueprint $table) { $table->id(); $table->foreignId('points_id'); $table->foreignId('user_id'); $table->unsignedTinyInteger('status'); $table->timestamp('created_at')->useCurrent(); $table->foreign('user_id')->references('id')->on('users'); $table->foreign('points_id')->references('id')->on('points'); $table->unique(['points_id', 'user_id', 'status']); }); } I need to make a factory to create a lot…

VIEW QUESTION

Laravel use randomly created factory for relation

I'm trying to seed random belongsTo relationships for a Post, but they are all just being created with the same User and Community. Code: $users = AppModelsUser::factory(100) ->create(); $communities = AppModelsCommunity::factory(10) ->create(); AppModelsPost::factory() ->for($users->random()->first()) ->for($communities->random()->first()) ->create(); After seeding, when I…

VIEW QUESTION
Back To Top
Search