简体   繁体   English

Laravel Nova列中where子句不明确/ where子句中的未知列

[英]Laravel Nova Column in where clause is ambiguous/Unknown column in where clause

  • Laravel Version: 5.8 Laravel版本:5.8
  • Nova Version: 2.0.0 Nova版本:2.0.0
  • PHP Version: 7.2.1 PHP版本:7.2.1
  • Operating System and Version: Max OSX 10.13.6 操作系统和版本:Max OSX 10.13.6
  • Browser type and version: Firefox 66.0.3 浏览器类型和版本:Firefox 66.0.3

Description: 描述:

Hi, I have 2 models relevant to this bug; 嗨,我有2个与此错误相关的模型; Event and TimeSlot. 事件和时间段。 They have a BelongsToMany relationship with each other. 他们彼此之间具有BelongsToMany关系。

I've created an index query so that only the time slots associated with the available events are retrieved. 我创建了一个索引查询,以便仅检索与可用事件关联的时隙。

The problem is that if I use return $query->whereIn('id',$time_slot_ids); 问题是,如果我使用return $query->whereIn('id',$time_slot_ids); I get a message on the Event entry that says SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous . 我在事件条目上收到一条消息,内容为SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous

屏幕截图2019-04-12 at 13 47 09

If I instead use return $query->whereIn('time_slot_id',$time_slot_ids); 如果我改用return $query->whereIn('time_slot_id',$time_slot_ids); the query works on the Event and returns the attached time slots correctly. 该查询对事件起作用,并正确返回附加的时隙。 However this in turn leads to an error message on the Time Slots page which reads SQLSTATE[42S22]: Column not found: 1054 Unknown column 'time_slot_id' in 'where clause' . 但是,这反过来又导致在“时隙”页面上显示一条错误消息,该消息显示为SQLSTATE[42S22]: Column not found: 1054 Unknown column 'time_slot_id' in 'where clause'

屏幕截图2019-04-12 at 13 47 49

My full block of code; 我完整的代码块;

/**
     * Build an "index" query for the given resource.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public static function indexQuery(NovaRequest $request, $query)
    {

        if(auth()->user()->userLevel->level == 'Group') {

            $time_slot_ids = [];

            foreach(auth()->user()->group->events as $event) {

                foreach($event->timeSlots as $timeSlot) {

                    $time_slot_ids[] = $timeSlot->id;

                }

            }

            return $query->whereIn('time_slot_id',$time_slot_ids);

        }

    }

Steps To Reproduce: 重现步骤:

Create a User, UserLevel, Group, Event and Time Slot model and accompanying Nova resource. 创建一个用户,用户级别,组,事件和时隙模型以及相应的Nova资源。

User belongs to a User Level. 用户属于用户级别。 User belongs to a Group. 用户属于一个组。 User belongs to many Events. 用户属于许多事件。 User belongs to many Time Slots. 用户属于多个时隙。

User Level has many Users. 用户级别有许多用户。

Group has many Users. 组有许多用户。 Group has many Events. 小组有很多活动。

Event belongs to a Group. 事件属于一个组。 Event belongs to many Users. 事件属于许多用户。 Event belongs to many Time Slots. 事件属于多个时隙。

Time Slot belongs to many Events. 时隙属于许多事件。 Time Slot belongs to many Users. 时隙属于许多用户。

Attempt to create an index query where only the time slots attached to the authenticated user's group's events are returned. 尝试创建索引查询,在该查询中仅返回附加到已验证用户组事件的时隙。

Am I doing something wrong? 难道我做错了什么?

Here's the full list of queries executed before I get the first error; 这是在我遇到第一个错误之前执行的查询的完整列表。

Laravel Nova查询

When querying multiple tables at once, you have to specify the table names for common column names. 一次查询多个表时,必须为公用列名指定表名。 So when querying the id, use this: 因此,在查询ID时,请使用以下代码:

return $query->whereIn('time_slots.id',$time_slot_ids);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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