douchun3680 2018-05-16 00:53
浏览 741
已采纳

通过将查询结果放入foreach中的另一个查询,从Laravel获取结果

I want to get results from Laravel by putting the results from the query into another query in foreach.

$first = DB::connection('store')
->table('entire_store')
->select('name', 'code', 'category', 'direct')
->where('open', 'Y')
->get();

The first query takes the information of the points and processes them into an array.

Match the information of the branches with the workplace in the employee table.

I want to save the workplace information of the employees together.

$temp = array();    
$i =0;    
foreach($first as $key => $item){

    $temp[$i]['name'] = $item->name;
    $temp[$i]['code'] = $item->code;

    $temp[$i]['category'] = $item->category;
    $temp[$i]['direct'] = $item→direct;

    $second = DB::connection('store')
     ->table('entire_employer')
     ->SELECT('mgr_no','mgr_id', 'status')
     ->where('store',$item->code)->get();
    //I tried.
    foreach($second as $key2 => $item2){
        $temp[$i]['mgr_id'] = $item2->mgr_id;
    }
    $i++;

}

I want to store the values from this query in temp_array.

I get an error if I try to save the value of the second query and print the value of temp_array ().

Is there anything more I need to do to check temp_array ()?

  • 写回答

1条回答 默认 最新

  • doujuxin7392 2018-05-16 03:30
    关注

    What you are trying to do is a LEFT JOIN of the entire_employer table onto the entire_store table. That will do exactly what you're trying to do in your code: match the records of one table with the records on the other; a LEFT join will anchor those records in the primary table (entire_store, in this case).

    You can do this in a single query, as such:

    $data = DB::connection('store')
      ->table('entire_store')
      ->select(
          'entire_store.name', 
          'entire_store.code', 
          'entire_store.category', 
          'entire_store.direct', 
          'entire_employer.mgr_no', 
          'entire_employer.mgr_id', 
          'entire_employer.status'
      )
      ->leftJoin('entire_employer', 'entire_employer.store', '=', 'entire_store.id')
      ->where('entire_employer.open', 'Y')
      ->get();
    

    You can read more about joins here: https://www.sitepoint.com/understanding-sql-joins-mysql-database/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器