Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel Livewire多类别过滤器不工作_Php_Laravel_Laravel Livewire - Fatal编程技术网

Php Laravel Livewire多类别过滤器不工作

Php Laravel Livewire多类别过滤器不工作,php,laravel,laravel-livewire,Php,Laravel,Laravel Livewire,我试图在laravel中使用livewire进行简单的过滤,但是被多类别过滤器卡住了 // Filters public $filter = [ "title" => "", "rangeFrom" => "", "rangeTo" => "", "order_field" => "order

我试图在laravel中使用livewire进行简单的过滤,但是被多类别过滤器卡住了

// Filters
public $filter = [
    "title" => "",
    "rangeFrom" => "",
    "rangeTo" => "",
    "order_field" => "order_by_name_asc",
    "selectedCat" => [],
];
// method of query
else if(!empty($this->filter['selectedCat'])) {
        $categories = explode(',', $this->filter['selectedCat']);

        $products = Product::where('category_id',
                    function ($query) use ($categories) {
                                $query->whereIn('category_id', $categories);
                            })->limit($this->loadAmount)
                                ->get();
    }
// html[![enter image description here][1]][1]
<p class="mt-4">Catgegories</p>
    @foreach ($categories as $cat)
    <div class="flex" wire:key="{{ $cat->id }}">
        <input type="checkbox" id="{{ $cat->title }}"
                class="h-4 w-4 text-gray-700 border rounded mr-2"
                wire:model="filter.selectedCat"
                value="{{ $cat->id }}">
        <label for="{{ $cat->title }}">{{ $cat->title }}</label>
    </div>
    @endforeach
//过滤器
公共$filter=[
“标题”=>“”,
“rangeFrom”=>“”,
“rangeTo”=>“”,
“订单字段”=>“订单名称”,
“selectedCat”=>[],
];
//查询方法
否则(!empty($this->filter['selectedCat'])){
$categories=explode(“,”,$this->filter['selectedCat']);
$products=产品::其中('category_id',
函数($query)使用($categories){
$query->where('category_id',$categories);
})->限制($this->loadAmount)
->get();
}
//html[![在此处输入图像描述][1][1]

类别

@foreach($类别为$cat) {{$cat->title} @endforeach
最终结果如下图所示。。。

通过绑定到
selectedCat
嵌套数组中的
动态
元素,可以实现您的目标

wire:model=“filter.selectedCat.{{{$cat->id}”
这样做的目的是向数组中添加一个元素,其中
类别
id
,并且
为true,如果取消选择,则为false。然后,可以使用数组
来确定要过滤的
类别
,只需删除带有
值的元素即可

刀片文件


@foreach($this->categories as$category)
{{$category->title}
@endforeach
@foreach($this->results as$result)
{{$result->title}({{$result->category->title})
@endforeach
组件

public$filters=[
“类别”=>[],
];
公共函数getCategoriesProperty()
{
返回类别::all();
}
公共函数getResultsProperty()
{
if(空($this->filters['categories'])){
退货产品::全部();
}
//这就是我们删除带有假值的类别的地方
$this->filters['categories']=array_filter($this->filters['categories']);
返回产品::其中('category_id',array_key($this->filters['categories'])->get();
}
公共职能
{
返回视图('livewire.category过滤器');
}

只是一个提示,如果您绑定到属性,则不需要值att->value=“{{{$cat->id}}”谢谢,它正在工作,但每当我取消选中类别时,它都不会显示任何产品,我也尝试使用
if(!empty($this->filters['categories'))
不工作您的逻辑不正确,再看看我的例子。是的,这就是我告诉你的,根据你的逻辑,当没有检查类别时,它应该工作,但它显示没有products@AhmadHassan你在上面的评论中所说的并不是我在我的例子中所说的。这个例子很有效。如果没有选择类别,则显示所有产品。我想我说过我也尝试过;)