Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mstdmstd's avatar

How with factory create new forum post items only with 1 is_best_decision = true for any thread?

Hello, In laravel 7 app I have 2 related tables ForumThread:

        Schema::create('forum_threads', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->string('title', 255);
            $table->string('slug', 260)->unique();

            $table->integer('creator_id')->unsigned();
            $table->foreign('creator_id')->references('id')->on('users')->onUpdate('RESTRICT')->onDelete('CASCADE');

            $table->integer('forum_id')->unsigned();
            $table->foreign('forum_id')->references('id')->on('forums')->onUpdate('RESTRICT')->onDelete('CASCADE');

            $table->boolean('is_salved')->default(false);

            $table->integer('views')->unsigned()->default(0);
            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('updated_at')->nullable();
            $table->index(['created_at'], 'forum_threads_created_at_index');

            $table->index(['creator_id', 'title'], 'forum_threads_creator_id_title_index');
            $table->index(['forum_id', 'is_salved'], 'forum_threads_forum_id_is_salved_index');
        });

and ForumPost:

        Schema::create('forum_posts', function (Blueprint $table) {
            $table->bigIncrements('id')->unsigned();

            $table->mediumText('body');
            $table->boolean('is_best_decision')->default(false);

            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onUpdate('RESTRICT')->onDelete('CASCADE');

            $table->integer('forum_thread_id')->unsigned();
            $table->foreign('forum_thread_id')->references('id')->on('forum_threads')->onUpdate('RESTRICT')->onDelete('CASCADE');

            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('updated_at')->nullable();
            $table->index(['created_at'], 'forum_posts_created_at_index');
            $table->index(['user_id', 'is_best_decision'], 'threads_user_id_is_best_decision_index');

        });

any ForumThread can have many ForumPost and ForumThread is considered as salved if one of its ForumPost items have is_best_decision = true. In this case ForumThread.is_salved is set to true

I need to add new forum post items with this rule for some ForumThreads. some of them are left as not salved.

in seeder I do :

        $forumThreads = ForumThread
            ::orderBy('id', 'asc')
            ->get();

        foreach ($forumThreads as $nextForumThread) {
            $ForumPostParams= [
                'user_id'          => rand(1, 9),
                'is_best_decision' => (rand(1, 3) == 1) ? '1' : '0',
                'forum_thread_id'  => $nextForumThread->id
            ];
            factory(App\ForumPost::class, rand(1, 9))->create($ForumPostParams);
        } 

and in database/factories/ForumPostFactory.php :

$factory->define(ForumPost::class, function (Faker $faker, $parentParams) {
    return [ 
        'body' => $faker->paragraphs(rand(2, 8), true),
        'is_best_decision' => $parentParams['is_best_decision'],
        'user_id' => rand(1,9),
        'forum_thread_id' => $parentParams['forum_thread_id'],
    ];
});

and it works ok, the only thing I need only for forum posts with same forum_thread_id only 1 field is_best_decision can be true.

How can I do it?

Thanks!

0 likes
0 replies

Please or to participate in this conversation.