简体   繁体   English

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

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

in my laravel app I'm gtting the following error, I think it's because the relation is incorrectly formed?在我的 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)\

Method triggering the error.触发错误的方法。

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

User Model:用户 Model:

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

Badge Model:徽章 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');
    }

Bage table migration贝奇表迁移

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();
        });


    }

User Badge Pivot table migration用户徽章 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');
    }
}

in your badge query you need to specify the table since you're using whereHas在您的徽章查询中,您需要指定表格,因为您使用的是 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.

相关问题 Laravel 完整性约束违规:1052 列 'id' in where 子句不明确 - Laravel Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous Laravel 6 错误:SQLSTATE[23000]:违反完整性约束:1052 where 子句中的列“id_perusahaan”不明确 - Laravel 6 Error : SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id_perusahaan' in where clause is ambiguous 完整性约束违规:1052 where子句中的列'id'不明确 - Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous 违反完整性约束:1052 where子句不明确的列'prof_id'Laravel - Integrity constraint violation: 1052 Column 'prof_id' in where clause is ambiguous Laravel Laravel Eloquent:违反完整性约束:where 子句中的 1052 列“id”不明确 - Laravel Eloquent: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous 如何解决完整性约束违规:1052 列 'id' in where 子句在 laravel 中不明确 - How to solve Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous in laravel Laravel Eloquent SQLSTATE[23000]:违反完整性约束:1052 列...在 where 子句中不明确 - Laravel Eloquent SQLSTATE[23000]: Integrity constraint violation: 1052 Column ... in where clause is ambiguous 完整性约束违规:1052 列 'group_id' 在 where 子句不明确 - Integrity constraint violation: 1052 Column 'group_id' in where clause is ambiguous 违反完整性约束:1052 order子句中的“位置”列不明确 - Integrity constraint violation: 1052 Column 'position' in order clause is ambiguous SQLSTATE [23000]:违反完整性约束:1052 where 子句中的列“值”不明确 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'value' in where clause is ambiguous
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM