【问题标题】:Why in pivot table result is different as I expected?为什么数据透视表结果与我预期的不同?
【发布时间】:2023-02-02 01:39:39
【问题描述】:

在 laravel 9 应用程序中,我与表创建了多对多关系 返回新类扩展迁移{

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'));


}

在 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']);
}

在 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']);
}

运行请求:

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

我得到了 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  

但结果与我预期的不同,因为在 article_vote 表中我有行:https://prnt.sc/wTE5uaPrQu9v

但我看到与 sql 不同:https://prnt.sc/Os14x5K6unyu

为什么 4 行具有不同的投票 ID?

谢谢!

【问题讨论】:

    标签: laravel eloquent pivot-table


    【解决方案1】:

    比较您的两个屏幕截图,似乎是不同的数据库,请查看pivot tablecreated_atupdated_at 值,它们在所有行中都完全不同。这可能是您正在查询的错误,例如,本地数据库与实时数据库?

    【讨论】:

    • 数据透视表 id 看起来也不同,第一个屏幕截图显示了一个数据透视表,其中的 id 从 1-4 和第二个显示从 11 到 19(有间隙)
    猜你喜欢
    • 2020-04-07
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    相关资源
    最近更新 更多