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

问题描述 投票:1回答:1
  • Laravel版本:5.8
  • Nova版本:2.0.0
  • PHP版本:7.2.1
  • 操作系统和版本:Mac OS 10.13.6
  • 浏览器类型和版本:Firefox 66.0.3

Description:

嗨,我有两个与这个bug相关的模型;事件和TimeSlot。他们彼此之间有BelongsToMany关系。

我创建了一个索引查询,以便只检索与可用事件关联的时隙。

问题是,如果我使用return $query->whereIn('id',$time_slot_ids);,我会在事件条目上收到SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous的消息。

如果我改为使用return $query->whereIn('time_slot_id',$time_slot_ids);,则查询将在Event上运行并正确返回附加的时隙。然而,这反过来导致时间槽页面上的错误消息读取SQLSTATE[42S22]: Column not found: 1054 Unknown column 'time_slot_id' in 'where clause'

我的完整代码块;

/**
     * 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:

创建User,UserLevel,Group,Event和Time Slot模型以及随附的Nova资源。

用户属于用户级别。用户属于某个组。用户属于许多事件。用户属于许多时隙。

用户级别有很多用户。

集团有很多用户。集团有很多活动。

活动属于某个组。事件属于许多用户。事件属于许多时段。

时间段属于许多事件。 Time Slot属于很多用户。

尝试创建索引查询,其中仅返回附加到经过身份验证的用户的组事件的时隙。

难道我做错了什么?

这是我收到第一个错误之前执行的查询的完整列表;

Laravel Nova Queries

php laravel laravel-nova laravel-5.8
1个回答
1
投票

一次查询多个表时,必须指定公用列名的表名。所以在查询id时,请使用:

return $query->whereIn('time_slots.id',$time_slot_ids);
© www.soinside.com 2019 - 2024. All rights reserved.