首页 > 解决方案 > 在 livewire 中为 QueryString 传递两个值

问题描述

标签: laravellaravel-livewire

解决方案


您可能需要满足一些场景。

1.单值

?filter_type=1

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

2.多个值(或)

?filter_type=1,2

返回type等于1或的所有记录2。的值filter_type将是一个字符串"1,2"。您可以通过逗号拆分字符串以获得实际所需的值。

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

3.多个值(和)

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

返回typeis1和的所有记录2(在这种情况下没有意义,但你明白了)。的值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
}

推荐阅读