Laravel集合获取空数组,但是sql给出正确的结果

时间:2019-06-18 08:33:40

标签: php mysql laravel-5 eloquent mysql-workbench

我想从数据库中获取一些结果,并且在phpmyadmin中运行时,原始sql查询可以提供正确的输出,但是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 ?

其中有什么问题????

1 个答案:

答案 0 :(得分:0)

尝试一下:

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

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

我希望对您有帮助