【问题标题】:Laravel Livewire Multiple Category Filter not WorkingLaravel Livewire 多类别过滤器不起作用
【发布时间】:2021-08-22 16:27:58
【问题描述】:

我正在尝试在 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

下图中的最终结果...

【问题讨论】:

  • 只是一个提示,如果你绑定到一个属性不需要值 att -> value="{{ $cat->id }}"

标签: php laravel laravel-livewire


【解决方案1】:

首先我们删除具有错误值的类别

public function getResultsProperty()
{

// First we remove the categories with a false value
$this->filters['categories'] = array_filter($this->filters['categories']);

if (empty($this->filters['categories'])) {
    return Product::all();
}

return Product::whereIn('category_id', array_keys($this->filters['categories']))->get();

}

【讨论】:

    【解决方案2】:

    您可以通过绑定到 dynamic 嵌套数组中的 dynamic 元素来实现您所追求的目标。

    wire:model="filter.selectedCat.{{ $cat->id }}"
    

    这样做是向数组中添加一个元素,其中keycategoryidvalue 为真,或者一旦取消选择它为假。然后可以使用数组keys 来确定要过滤哪个categories,只需删除具有false 值的元素即可。

    刀片文件

    <div>
        @foreach ($this->categories as $category)
            <div class="flex items-center space-x-4 mb-2" wire:key="{{ $category->id }}">
                <input type="checkbox" id="{{ $category->title }}" name="{{ $category->title }}"
                    wire:model="filters.categories.{{ $category->id }}" />
                <label for="{{ $category->title }}">{{ $category->title }}</label>
            </div>
        @endforeach
    
        <div class="grid grid-cols-4 gap-4">
            @foreach ($this->results as $result)
                <div>{{ $result->title }} ({{ $result->category->title }})</div>
            @endforeach
        </div>
    </div>
    

    组件

    public $filters = [
        'categories' => [],
    ];
    
    public function getCategoriesProperty()
    {
        return Category::all();
    }
    
    public function getResultsProperty()
    {
        if (empty($this->filters['categories'])) {
            return Product::all();
        }
    
        // this is where we remove the categories with a false value
        $this->filters['categories'] = array_filter($this->filters['categories']);
    
        return Product::whereIn('category_id', array_keys($this->filters['categories']))->get();
    }
    
    public function render()
    {
        return view('livewire.category-filter');
    }
    

    【讨论】:

    • 谢谢,它正在工作,但是每当我取消选中它显示没有产品的类别时,我也尝试使用 if(!empty($this-&gt;filters['categories'])) 不工作
    • 你的逻辑不对,再看我的例子。
    • 是的,这就是我告诉你的,根据你的逻辑,当没有检查类别但它显示没有产品时它应该工作
    • @AhmadHassan 你在上面的评论中所说的不是我在我的例子中所说的。该示例有效。如果没有选择任何类别,则显示所有产品。
    • 那你做错了什么。请使用您正在使用的确切代码更新您的问题。
    猜你喜欢
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 2021-05-04
    • 2020-12-16
    • 2021-07-18
    • 2021-08-14
    • 2014-04-12
    • 1970-01-01
    相关资源
    最近更新 更多