繁体   English   中英

Laravel SQL 错误:违反完整性约束:1052 where 子句中的列“id”不明确

[英]Laravel SQL Error: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous

在我的 laravel 应用程序中,我收到以下错误,我认为是因为关系格式不正确?

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select * from `badges` where exists (select * from `users` inner join `user_badge` on `users`.`id` = `user_badge`.`user_id` where `badges`.`id` = `user_badge`.`badge_id` and `id` = 1) and `badgecategory_id` = 4 limit 1)\

触发错误的方法。

public function syncBadges(Request $request)
    {

        $user = User::with('badges')->findOrFail($request['user_id']);

        $badge = Badge::whereHas('users', function ($query) use ($request){
            $query->where('id' , $request['user_id']);
        })
        ->where('badgecategory_id',$request['badgecategory_id'])
        ->first();

        if($badge)
        {
            $user->badges()->dettach($badge['id']);
            $user->badges()->attach($request['badge_id']);
        }
        else
        {
            $user->badges()->attach($request['badge_id']);
        }
}

用户 Model:

public function badges()
    {
        return $this->belongsToMany('App\Models\Badge', 'user_badge', 'user_id', 'badge_id');
    }

徽章 Model:

protected $table = 'badges';

public function users()
    {
        return $this->belongsToMany('App\Models\User', 'user_badge', 'badge_id', 'user_id');
    }

    public function category()
    {
        return $this->belongsTo('App\Models\BadgeCategory', 'badgecategory_id');
    }

贝奇表迁移

public function up()
    {


        Schema::create('badges', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('badgecategory_id')->index();
            $table->foreign('badgecategory_id')->references('id')->on('badgecategories')->onDelete('cascade')->onUpdate('cascade');
            $table->string('title');
            $table->string('icon')->nullable();
            $table->timestamps();
        });


    }

用户徽章 Pivot 表迁移

class CreateUserBadgeTable extends Migration
{
    public function up()
    {


        Schema::create('user_badge', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id')->index()->nullable();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
            $table->unsignedInteger('badge_id')->nullable()->index();
            $table->foreign('badge_id')->references('id')->on('badges')->onDelete('cascade')->onUpdate('cascade');
            $table->timestamps();
        });


    }

    
    public function down()
    {
        DB::statement('SET FOREIGN_KEY_CHECKS = 0');
        Schema::dropIfExists('user_badge');
        DB::statement('SET FOREIGN_KEY_CHECKS = 1');
    }
}

在您的徽章查询中,您需要指定表格,因为您使用的是 whereHas

$badge = Badge::whereHas('users', function ($query) use ($request){
        $query->where('users.id' , $request['user_id']);
    })
    ->where('badgecategory_id',$request['badgecategory_id'])
    ->first();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM