簡體   English   中英

Laravel Eloquent:違反完整性約束:where 子句中的 1052 列“id”不明確

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

我正在建立一個網站,許多成員可以加入許多團體。

//User.php
public function groups()
    {
        return $this->belongsToMany(Group::class, 'group_member', 'member_id', '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();
    }

正在從 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);
    }

它返回

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

在最后一個 where 子句中,有“ id=1 ”。 您需要將其表示為“ table_name .id=1”。

我不確定但它可能會起作用

你的代碼

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

你能dd($members)並分享結果,所以我會深入研究這個問題

我忘了我已經為 Group 模型實現了一個全局范圍。 在 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);
        }
    }
}

就我而言,這是由於多租戶TenantScope范圍。

解決方法是在字段前面加上表名——在我的例子中是通用的——因為范圍適用於所有模型。

我的解決方案是使用$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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM