Home > other >  Why in pivot table result is different as I expected?
Why in pivot table result is different as I expected?

Time:01-29

In laravel 9 app I create many top many relation with table return new class extends Migration {

public function up()
{
    Schema::create('article_vote', function (Blueprint $table) {
        $table->id();
        $table->foreignId('article_id')->references('id')->on('articles')->onUpdate('RESTRICT')->onDelete('CASCADE');
        $table->foreignId('vote_id')->references('id')->on('votes')->onUpdate('RESTRICT')->onDelete('CASCADE');

        $table->boolean('active')->default(false);
        $table->date('expired_at')->nullable();

        $table->integer('supervisor_id')->nullable()->unsigned();
        $table->foreign('supervisor_id')->references('id')->on('users')->onDelete('CASCADE');
        $table->mediumText('supervisor_notes')->nullable();

        $table->timestamp('created_at')->useCurrent();
        $table->timestamp('updated_at')->nullable();

        $table->unique(['vote_id', 'article_id'], 'article_vote_vote_id_article_id_index');
        $table->index(['vote_id', 'article_id', 'active', 'expired_at'], 'article_vote_vote_id_article_id_active_expired_at_index');
        $table->index([ 'expired_at', 'active',], 'article_vote_expired_at_active_index');
        $table->index(['created_at'], 'article_vote_created_at_index');
    });

    Artisan::call('db:seed', array('--class' => 'articleVotesWithInitData'));


}

In app/Models/Vote.php :

public function articles(): BelongsToMany
{
    return $this->belongsToMany(Article::class, 'article_vote', 'vote_id')
                ->withTimestamps()
                ->withPivot(['active', 'expired_at', 'supervisor_id', 'supervisor_notes']);
}

and in app/Models/Article.php :

public function votes(): BelongsToMany
{
    return $this->belongsToMany(Vote::class, 'article_vote', 'article_id')
        ->withTimestamps()
        ->withPivot(['active', 'expired_at', 'supervisor_id', 'supervisor_notes']);
}

Running requests:

$article = Article::getById(2)
    ->firstOrFail();
$articleVotes = $article->votes;

I got sql :

   SELECT `votes`.*, `article_vote`.`article_id`     AS `pivot_article_id`, `article_vote`.`vote_id`     AS `pivot_vote_id`, `article_vote`.`created_at`     AS `pivot_created_at`, `article_vote`.`updated_at`     AS `pivot_updated_at`, `article_vote`.`active`     AS `pivot_active`, `article_vote`.`expired_at`     AS `pivot_expired_at`, `article_vote`.`supervisor_id`     AS `pivot_supervisor_id`, `article_vote`.`supervisor_notes`     AS `pivot_supervisor_notes` 
    FROM `votes` 
    INNER JOIN `article_vote` on `votes`.`id` = `article_vote`.`vote_id` 
    WHERE `article_vote`.`article_id` = 2  

But result is different as I expected, as in article_vote table I have rows : https://prnt.sc/wTE5uaPrQu9v

But I see different with the sql : https://prnt.sc/Os14x5K6unyu

Why 4 rows with different vote id ?

Thanks!

CodePudding user response:

Comparing your two screenshots, it seems like different databases, look at the created_at and updated_at values of the pivot table, they are totally different for all the rows. Could that be a mistake that you're querying, for example, local vs live DBs?

  • Related