在 livewire 中为 QueryString 传递两个值

passing two value for QueryString in livewire

我使用 Livewire 并为 table 添加了一个过滤器。 我的组件具有以下代码:

    use WithPagination;
    public bool $loadData = false;
    public $filter_type = null;
    protected $queryString = ['filter_type'];
    

    public function setType($type)
    {
        $this->filter_status = $type;
    }
    
    public function render()
    {
            $transactions = Transaction::when($this->filter_type, function ($query){
                    $query->where('type' , $this->filter_type);
                })
                ->orderBy('created_at', 'DESC')
                ->get();
          return view('livewire.backend.financial.transactions')->with('transactions' , collect($transactions)->paginate(10));
    }

当我应用过滤器时。本站地址如下

https://example.com/table?filter_type=0

现在我要给filter_type‍‍两个值(比如0和1) 也就是说,显示类型为 0 和 1 的项。 我该怎么做?

您可能想要满足一些情况。

1.单值

?filter_type=1

Return 只是 type 等于 1 的记录。 filter_type 的值将是一个字符串 "1".

2。多个值(或)

?filter_type=1,2

Return type 等于 12 的所有记录。 filter_type 的值将是一个字符串 "1,2"。您可以用逗号分隔字符串以获得实际需要的值。

$values = explode(',', $filter_type);

3。多个值(和)

?filter_type[]=1&filter_type[]=2

Return type12 的所有记录(在这种情况下没有意义,但您明白了)。 filter_type 的值将是一个数组 [0 => 1, 1 => 2].

以上只是建议,但让我们假设您选择 option 2。您可以按照以下方式实施它;

public function render()
{
    $types = explode(',', $this->filter_type);

    $transactions = Transaction::when($this->filter_type, function ($query) use ($types) {
        $query->whereIn('type', $types);
    })
    ->orderBy('created_at', 'DESC')
    ->get();

    // do what you want with the $transactions
}