简体   繁体   English

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

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

I am building a website where many members can join many groups.我正在建立一个网站,许多成员可以加入许多团体。

//User.php
public function groups()
    {
        return $this->belongsToMany(Group::class, 'group_member', 'member_id', 'group_id');
    }

The function to retrieve related members from the group id从组id中检索相关成员的函数

//UserRepository.php
public function getRelatedMembersByGroupIds(array $groupIds)
    {
        $members = User::whereHas('groups', function ($query) use ($groupIds) {
            return $query->whereIn('groups.id', $groupIds);
        });
        return $members->get();
    }

The repository is being called from the UserController正在从 UserController 调用存储库

//UserController.php
public function getRelatedMembersByGroupIds(Request $request)
    {
        $groupIds = $request->get('group_ids');

        $members = $this->userRepository->getRelatedMembersByGroupIds($groupIds);

        $responses = [
            'status' => 'success',
            'groupRelatedMembers' => $members
        ];

        return response()->json($responses);
    }

It returns它返回

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select users.* from users where exists (select * from groups inner join group_member on groups.id = group_member.group_id where users.id = group_member.member_id and groups.id in (1) and groups.deleted_at is null and id = 1)) SQLSTATE[23000]:违反完整性约束:1052 列“id”在 where 子句中不明确(SQL:select users.* from users where exists (select * from groupsinner join group_member on groups.id = group_member.group_id where users.id = group_member.member_id and groups.id in (1) and groups.deleted_at 为空且id = 1))

In last where clause, there is " id=1 ".在最后一个 where 子句中,有“ id=1 ”。 You need to express it as " table_name .id=1".您需要将其表示为“ table_name .id=1”。

i am not daam sure but it may work我不确定但它可能会起作用

Your Code你的代码

public function getRelatedMembersByGroupIds(array $groupIds)
    {
        $members = User::whereHas('groups', function ($query) use ($groupIds) {
            return $query->whereIn('groups.id', $groupIds);
        });
        return $members->get();
    }

Can You dd($members) and share the result so i will dig deep into the issue你能dd($members)并分享结果,所以我会深入研究这个问题

I forgot I had implemented a global scope for Group model.我忘了我已经为 Group 模型实现了一个全局范围。 After adding groups.id in the GroupScope, It worked fine.在 GroupScope 中添加 groups.id 后,它工作正常。

<?php

namespace App\Scopes;

use App\Helpers\AuthenticationHelper;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class GroupScope implements Scope
{
    /**
     * Apply the scope to a given Eloquent query builder.
     *
     * @param \Illuminate\Database\Eloquent\Builder $builder
     * @param \Illuminate\Database\Eloquent\Model $model
     * @return void
     */
    public function apply(Builder $builder, Model $model)
    {
        if (AuthenticationHelper::isEmployee())
        {
            //Problem was I did not added groups table below.
            $builder->where('groups.id', auth()->user()->branch_id);
        }
    }
}

In my case, it was due to multi-tenant TenantScope scope.就我而言,这是由于多租户TenantScope范围。

The fix was to prefix the field with the table name - which was generic in my case - as the scope is applied to all models.解决方法是在字段前面加上表名——在我的例子中是通用的——因为范围适用于所有模型。

My solution was to use $model->getTable() as below:我的解决方案是使用$model->getTable()如下:

<?php

namespace App\Scopes;

use App\Helpers\AuthenticationHelper;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class GroupScope implements Scope
{
    /**
     * Apply the scope to a given Eloquent query builder.
     *
     * @param \Illuminate\Database\Eloquent\Builder $builder
     * @param \Illuminate\Database\Eloquent\Model $model
     * @return void
     */
    public function apply(Builder $builder, Model $model)
    {
        if (AuthenticationHelper::isEmployee())
        {
            //Problem was I did not added groups table below.
            $builder->where($model->getTable() . '.id', auth()->user()->branch_id);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循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 完整性约束违规:1052 where子句中的列&#39;id&#39;不明确 - Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous Laravel Eloquent SQLSTATE[23000]:违反完整性约束:1052 列...在 where 子句中不明确 - Laravel Eloquent SQLSTATE[23000]: Integrity constraint violation: 1052 Column ... in where clause is ambiguous 违反完整性约束:1052 where子句不明确的列&#39;prof_id&#39;Laravel - Integrity constraint violation: 1052 Column 'prof_id' in where clause is ambiguous Laravel 如何解决完整性约束违规:1052 列 &#39;id&#39; in where 子句在 laravel 中不明确 - How to solve Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous in laravel 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 Laravel SQL 错误:违反完整性约束:1052 where 子句中的列“id”不明确 - Laravel SQL Error: Integrity constraint violation: 1052 Column 'id' 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