简体   繁体   English

如何在laravel 5.4中的两个表上进行左联接

[英]how to make left join on two tables in laravel 5.4

These are my two table's as: 这是我的两个表,如:

company_industry_type: company_industry_type:

在此处输入图片说明

law_master : law_master 在此处输入图片说明

table company_industry_type has columns lm_id that is assigned to particular id. 表company_industry_type具有分配给特定ID的lm_id列。

i want to fetch the lm_id and law_name from table law_master with respect to the lm_id assigned to id of table company_industry_type 我想相对于分配给表company_industry_type的 ID的lm_id从表law_master提取lm_id和law_name

please help me with this, i'm new to laravel. 请帮我解决这个问题,我是laravel的新手。

<?php
  $law = DB::table('tbl_company_industry_type')->pluck('lm_id');
  $law_d = DB::table('tbl_law_master')->whereIn('id',$law)
           ->select('id','lm_id','law_name')->get();
           $res_lms = '';
           foreach($law_d as $law_details)
           {
           ?>
           <span id="sublaw_data">{{ $law_details->lm_id }} 
           ({{ $law_details->law_name }}) <i class="fa fa-remove deleteclass" 
           onclick="delete_law('<?php echo $law_details->id?>')"></i></span>
           <?php
           $res_lms .=$law_details->id.",";
           }
           $res_lawids=trim($res_lms,',');
?>

my code returns only one id's data ie 1 and not for 3,4 for last record of company_industry_type 我的代码仅返回一个id的数据,即1,而不是针对company_industry_type的最后记录的3,4

Using below query you can get result as per your requirements. 使用以下查询,您可以根据需要获取结果。

DB::table('company_industry_type') ->join('company_industry_type', 'company_industry_type.lm_id', '=', 'law_master.lm_id') ->select('law_master.lm_id', 'law_master.law_name', 'company_industry_type.*') ->get(); DB :: table('company_industry_type')-> join('company_industry_type','company_industry_type.lm_id','=','law_master.lm_id')-> select('law_master.lm_id','law_master.law_name',' company_industry_type。*')-> get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM