php - Laravel 集合获取空数组但 sql 给出了正确的结果
问题描述
我想从数据库中获取一些结果,原始 sql 查询在 phpmyadmin 中运行时会给出正确的输出,但 laravel 会给出空数组。
$transectionDetails=Wallet_transaction::query();
$transectionDetails->select('kitchen_name','parent_id',DB::raw("CONCAT(f_name, ' ', l_name) AS name"),'transaction_amount','transaction_time','transaction_id')
->join('parent_details','wallet_transactions.parent_id', '=','parent_details.id')
->join('kitchens','parent_details.kitchen_id', '=','kitchens.id')
->whereNotNull('wallet_transactions.transaction_id')
->where('wallet_transactions.transaction_for',2);
if(!empty($post_data) && $post_data['parent_name'] != ''){
$transectionDetails->where(DB::raw("CONCAT(f_name, ' ', l_name)"), 'like', "'%".$post_data['parent_name'] ."%'");
// echo '<pre>';var_dump(get_class_methods($transectionDetails));
echo '<pre>';var_dump($transectionDetails->toSql());
echo '<pre>';var_dump($transectionDetails->get());die;
}
var_dump($transectionDetails->get());die;
这给出了空数组,但数据库给出了正确的数组。
var_dump($transectionDetails->toSql());
给出:
select `kitchen_name`, `parent_id`, CONCAT(f_name, ' ', l_name) AS name,
`transaction_amount`, `transaction_time`, `transaction_id` from
`wallet_transactions` inner join `parent_details` on
`wallet_transactions`.`parent_id` = `parent_details`.`id` inner join
`kitchens` on `parent_details`.`kitchen_id` = `kitchens`.`id` where
`wallet_transactions`.`transaction_id` is not null and
`wallet_transactions`.`transaction_for` = ? and CONCAT(f_name, ' ',
l_name) like ?
有什么问题???**
解决方案
尝试这个:
$transectionDetails = Wallet_transaction::query();
$transectionDetails = $transectionDetails->select('kitchen_name','parent_id',DB::raw("CONCAT(f_name, ' ', l_name) AS name"),'transaction_amount','transaction_time','transaction_id')
->join('parent_details','wallet_transactions.parent_id', '=','parent_details.id')
->join('kitchens','parent_details.kitchen_id', '=','kitchens.id')
->whereNotNull('wallet_transactions.transaction_id')
->where('wallet_transactions.transaction_for',2);
if(!empty($post_data) && $post_data['parent_name'] != '') {
$transectionDetails = $transectionDetails->where(DB::raw("CONCAT(f_name, ' ', l_name)"), 'like', "'%".$post_data['parent_name'] ."%'");
$results = $transectionDetails->get();
dd($results);
}
您需要使用您对其应用的修改来更新您的查询变量。
我希望它有帮助
推荐阅读
- javascript - Lodash findKey() 方法
- redis - redis 是否提供了对缓存对象进行部分更新的选项
- typescript - 来自对象的所有嵌套值的类型
- javascript - 带有 javascript 标签的 Youtube iframe API
- django - 如何对特定字段求和
- flutter - Flutter 中的 3D 轮播动画
- button - 为什么我的 Button 样式对象仅适用于按钮内的文本,而不适用于整个按钮?
- docker - 在 Windows 上使用 docker-compose 增加 Docker 容器的内存?
- powershell - 如何从 CSV 文件中的 DistinguishedName 过滤“OU=Service account”
- python - Pandas 合并在大型数据集上非常慢