Laravel/Livewire:雄辩的查询将字符串更改为数组

问题描述 投票:0回答:1

我有一个非常令人困惑的问题,我真的很难通过谷歌/llm 进行搜索。

如果是 livewire 类,我有以下代码:

public function updated()
    {

        $agent = Agent::find($this->selectedAgentId);
        $this->selectedAgentName = $agent->name;
        $ext = $agent->getActiveExtByOccupancyDate($this->selectedDate)->id;
        Log::info("Ext: " . $ext);

        if ($ext) {

            $this->activities = ExtensionStatus::where('extension_id', '=', (string) $ext)
                ->whereDate('fs_time', '=', $this->selectedDate)
                ->get();

            Log::info($ext);

            $this->agentActiveOnSelectedDate = $this->activities->count() > 0;
        }
        else
        {
            $this->agentActiveOnSelectedDate = false;
        }
    }

日志可以双重确认

$ext
绝对是一个值,2727,确实如此。但是,当查询运行时,我收到以下错误:

SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: character varying = integer LINE 1: ...agent_status" where "agent_status"."extension_id" in (2727, ... ^ HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
select * from "agent_status" where "agent_status"."extension_id" in (2727, 2727, 2727)

有人能帮我诊断一下吗?为什么要传入数组?

编辑:请在否决之前告诉我这个问题有什么问题,以便我可以改进它。

php laravel laravel-livewire
1个回答
0
投票

我想说将extension_id的数据类型从字符串更改为整数(int/smallInt/bigInt)数据类型。然后在你的代码中,确认 $ext 不为 null 并且返回了一个整数值后,你可以得到诸如

之类的活动
$this->activities = ExtensionStatus::where('extension_id', $ext)
                ->whereDate('fs_time', $this->selectedDate)
                ->get();

注意:Laravel 默认情况下假定 where 方法上有“=”运算符。因此,您可以直接传递您想要比较的值,即 ($ext) 作为第二个参数。

© www.soinside.com 2019 - 2024. All rights reserved.