【问题标题】:Invalid parameter number error when using whereIn with livewire将 whereIn 与 livewire 一起使用时出现无效的参数编号错误
【发布时间】:2021-11-14 06:44:22
【问题描述】:

我正在尝试创建一个多选过滤器。当我使用 whereIn 作为 id 我得到这个错误

参数号无效(SQL:select * from stations where id in (16128))

16128 是为该查询选择的两个 ID 之一。它适用于手动给定的数组,所以我不确定问题是什么。我也尝试过使用 array_values,但得到了相同的结果。

            <label for="" class="col-md-3">Daypart</label>
                <div class="container-checkbox">
                    @foreach($dayparts as $id => $daypart)
                        <div wire:key="{{ $daypart->id }}">
                            <input type="checkbox" id="{{ $daypart->daypart_name }}" name="{{ $daypart->daypart_name }}" wire:model="filters.dayparts.{{ $daypart->id }}" wire:click="filtru({{ $daypart->id }})" />
                        </div>
                    @endforeach                       
                </div>
 public function filtru($id){
        
        $this->filters['dayparts'] = array_filter($this->filters['dayparts']);
    
        $dayp =  DaypartStation::whereIn('daypart_id', array_keys($this->filters['dayparts']))->get();
        foreach($dayp as $day){
            $this->arr[]=$day->station_id;
        }
        
        $toate = Station::whereIn('id', [$this->arr])->get();
        //dd(Station::whereIn('id', $this->arr)->get());
        //dd(Station::whereIn('id', [array_values($this->arr)])->get());
        // dd(Station::whereIn('id', [16576, 16776, 16376])->get());
    }
 public function render()
    {
        
        return view('livewire.create-workbooks-table', [
            'stations'=> Station::
                when($this->filters['dayparts'], function($query){
                    $query->whereIn('id', [$this->arr]);
                    })
     
            
                    ->search($this->search)
                    ->orderBy($this->sortBy, $this->sortDirection)
                    ->latest()
                    ->Paginate($this->perPage), 
 
        ]);
    }

【问题讨论】:

  • 您能在查询运行前检查$this-&gt;arr 的值吗?
  • 它返回正确的数组以及记录的 id。我有两个与车站相连的白天部分,一个是与一个车站相连的晚上,另一个是与两个车站相连的白天。 “晚上”的工作方式以某种方式工作,因为它只有一个 id,但“白天”给出了这个错误。数组:1 [▼ 0 => 15619 ] 和白天数组:2 [▼ 0 => 16128 1 => 16385 ] 所以应该提取数据

标签: laravel laravel-livewire


【解决方案1】:

我认为你应该改变

 $toate = Station::whereIn('id', [$this->arr])->get();

 $toate = Station::whereIn('id', $this->arr)->get();

因为$this-&gt;arr 已经是一个数组

【讨论】:

  • 它仍然没有改变结果,我尝试了任何一种方式
猜你喜欢
  • 1970-01-01
  • 2016-08-18
  • 2018-07-05
  • 2019-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-23
  • 2021-06-25
相关资源
最近更新 更多