我从存储过程中获取数据结果作为 Laravel 集合。
数据就像 SQL 表。集合对象中的相同键(列名)。
有没有办法查询(原始 SQL 查询)这个集合并得到我想要的最终结果数据?
我查看了 Laravel collection documentation我可以运行一些方法来查询集合,但这不是原始 SQL 查询(我需要对集合数据运行一些复杂的原始 SQL 查询。)
理想情况下,我需要根据收集的数据创建一个临时 SQL 表。集合可能会根据结果而有所不同,并且需要基于该结果创建临时表。
我还查看了
Redis 服务器
,我在其中临时存储我的集合并进行查询,但没有找到任何可以存储为 SQL 表并对其运行 SQL 查询的内容。第三个选项是将结果数据存储在
Web SQL
上并运行查询,但还需要下载从后端完成的 excel。
有人遇到过类似的问题吗?也许我可以用别的东西来解决这个问题。
最佳答案
您应该能够像这样使用模型进行查询:
//Query the db, and fetch collection with get()
$userCollection = User
::where('function_id', 3)
->get();
//Query the collection and fetch the first answer, resulting in a user model
$pietPaulusma = $userCollection
->where('firstname', 'Piet')
->where('lastname', 'Paulusma')
->where('email', 'piet@paulus.ma')
->first();
或者没有像这样的模型:
DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();
如果你学习this documentation ,你应该走得更远:)
希望对您有所帮助!
关于php - 如何查询laravel采集结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49372204/