首页 > 解决方案 > 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 ?

有什么问题???**

标签: phpmysqllaravel-5eloquentmysql-workbench

解决方案


尝试这个:

$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);
}

您需要使用您对其应用的修改来更新您的查询变量。

我希望它有帮助


推荐阅读