php Laravel口才:完整性约束冲突:1052 where子句中的列'id'不明确

jq6vz3qz  于 2023-03-16  发布在  PHP
关注(0)|答案(5)|浏览(181)

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

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

它回来了
数据库状态[23000]:完整性约束冲突:1052 where子句中的列'id'不明确(SQL:从存在的用户中选择用户.*(从组中选择 *,组内部加入www.example.com上的group_membergroups.id= group_member.group_id,其中users.id= group_member.member_idgroups.id和(1)中的www.example.com以及groups.deleted_at为空且id = 1))

h7appiyu

h7appiyu1#

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

ff29svar

ff29svar2#

我不敢肯定,但可能有用
您的代码

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

你能dd($members)和分享的结果,所以我会深入挖掘的问题

yacmzcpb

yacmzcpb3#

我忘了我已经为组模型实现了一个全局范围。groups.id在GroupScope中添加www.example.com后,它工作得很好。

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

ygya80vv4#

在我的例子中,这是由于多租户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);
        }
    }
}
px9o7tmv

px9o7tmv5#

我在多租户范围方面遇到了一个问题,这个更改对我很有效。
从这改变

$builder->where('tenant_id', session()->get('tenant_id'));

到这个

$builder->where($model->getTable() . '.tenant_id', session()->get('tenant_id'));


<?php

namespace App\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Notifications\Notifiable;

class TenantScope 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
     */
    use Notifiable;

    public function apply(Builder $builder, Model $model)
    {
        if (session()->has('tenant_id')) {
            $builder->where($model->getTable() . '.tenant_id', session()->get('tenant_id'));
        }
    }
}

非常感谢大家。

相关问题