laravel - 在 livewire 中为 QueryString 传递两个值
问题描述
解决方案
您可能需要满足一些场景。
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
返回type
is1
和的所有记录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
}
推荐阅读
- ecmascript-6 - 在模板文字中添加 JS 函数
- python - 如何从给定索引中剪切列表
- android - Android TV box MediaPlayer info/warning (8004, 0), error (1, -33554485)
- typescript - 为什么打字稿在使用逻辑运算符时将联合类型解析为可能的空字符串?
- javascript - 使用 innerHTML 设置文档 ID 时 Firestore 重复(空)文档 ID
- share - 是否有用于在 RingCentral Glip 中共享附件的 API?
- linux - 复制或移动结合源路径和目标路径,以避免重复长路径
- jsf - JSF 面临流抛出错误“流已存在”
- active-directory - 如何确保存储在数据库中的管理员密码的安全性以与 LDAP 同步
- excel - Excel VBA 组合框值条件