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

通过将查询结果放入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 ()?

图片转代码服务由CSDN问答提供 功能建议

我想通过将查询结果放入foreach中的另一个查询中来获取Laravel的结果。

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

第一个查询 获取点的信息并将它们处理成一个数组。

在employee表中将分支的信息与工作区匹配。

I 想要将员工的工作场所信息保存在一起。

  $ temp = array();  
 $ i = 0;  
foreach($ first as $ key => $ item){
 
 $ temp [$ i] ['name'] = $ item-> name; 
 $ temp [$ i] ['code'  ] = $ item->代码; 
 
 $ 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(); 
 //我试过。
 foreach($ second as $ key2 => $ item2){
 $ temp [  $ i] ['mgr_id'] = $ item2-> mgr_id; 
} 
 $ i ++; 
 
} 
   
 
 

我想存储 temp_array中此查询的值。

如果我尝试保存第二个查询的值并打印temp_array()的值,则会出错。 \ n

我还需要做些什么才能检查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/

    点赞 评论

相关推荐 更多相似问题