【发布时间】:2021-11-14 06:44:22
【问题描述】:
我正在尝试创建一个多选过滤器。当我使用 whereIn 作为 id 我得到这个错误
参数号无效(SQL:select * from
stationswhereidin (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->arr的值吗? -
它返回正确的数组以及记录的 id。我有两个与车站相连的白天部分,一个是与一个车站相连的晚上,另一个是与两个车站相连的白天。 “晚上”的工作方式以某种方式工作,因为它只有一个 id,但“白天”给出了这个错误。数组:1 [▼ 0 => 15619 ] 和白天数组:2 [▼ 0 => 16128 1 => 16385 ] 所以应该提取数据