简体   繁体   中英

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

//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.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))

In last where clause, there is " id=1 ". You need to express it as " 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

I forgot I had implemented a global scope for Group model. After adding groups.id in the GroupScope, It worked fine.

<?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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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