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

时间:2019-05-16 09:10:09

标签: php laravel eloquent

我正在建立一个网站,许多成员可以加入许多团体。

//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:选择用户。*从存在的用户处*   (从groups.id =的组内部连接group_member中选择*   group_member.group_id,其中users.id = group_member.member_id和   (1)中的groups.id和groups.deleted_at为null且id = 1))

4 个答案:

答案 0 :(得分:1)

最后一个where子句中有“ id = 1 ”。您需要将其表示为“ 表名称 .id = 1”。

答案 1 :(得分:0)

我不确定,但它可能有效

您的代码

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

您能dd($members)并分享结果吗,以便我深入探讨问题

答案 2 :(得分:0)

我忘了我已经实现了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);
        }
    }
}

答案 3 :(得分:0)

就我而言,这是由于多租户 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);
        }
    }
}