【问题标题】:Laravel collection fetch empty array but sql gives proper resultsLaravel 集合获取空数组但 sql 给出了正确的结果
【发布时间】:2019-10-31 20:23:59
【问题描述】:

我想从数据库中获取一些结果,并且原始 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-&gt;get());die;

这给出了空数组,但数据库给出了正确的数组。

var_dump($transectionDetails-&gt;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 = $transectionDetails-&gt;get()$transectionDetails = $transectionDetails-&gt;select(...) 然后var_dump

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


【解决方案1】:

试试这个:

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

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

希望对你有帮助

【讨论】:

  • 好的。谢谢 .where(DB::raw("CONCAT(f_name, ' ', l_name)"), 'like', "'%".$post_data['parent_name'] 。 "%'"); $results = $transsectionDetails->get();我将其更改为 WhereRaw,现在可以使用了。
  • @Arunima 完美,如果它帮助您解决问题,请将答案标记为已接受,以便我们可以在堆栈溢出时关闭此线程
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-13
相关资源
最近更新 更多