[英]Laravel Mysql JOIN not working
I have 2 tables: 我有2张桌子:
I need to get the table2 rows for each table 1 rows if table2.id=table1.id are equal. 如果table2.id = table1.id相等,我需要为每个表获取1行的table2行。
I need something like this: t1.row1=array("t2.row1","t2.row2" etc.); 我需要这样的东西:t1.row1 = array(“t2.row1”,“t2.row2”等);
I used this: 我用过这个:
$first= DB::table('resurse')
->join('alocare', function($join)
{
$join->on( 'resurse.id', '=','alocare.resursa');
})
->groupBy('id')->get();
var_dump($first);
the response is: 回应是:
array (size=2)
0 =>
object(stdClass)[159]
public 'id' => int 1
public 'denumire' => string 'APAD' (length=4)
public 'id_aloc' => int 1
public 'resursa' => int 1
public 'subunitate' => int 1
public 'status' => string 'btn btn-danger' (length=14)
1 =>
object(stdClass)[160]
public 'id' => int 2
public 'denumire' => string 'AD' (length=2)
public 'id_aloc' => int 3
public 'resursa' => int 2
public 'subunitate' => int 2
public 'status' => string 'btn btn-danger' (length=14)
According to you comment, i can assume these solutions to your question: Get data from database with join in Laravel. 根据您的评论,我可以假设您的问题的这些解决方案:通过加入Laravel从数据库获取数据。
DB::table('resurse')
->rightJoin('alocare', 'resurse.id', '=', 'alocare.resursa')
->groupBy('resurse.id')
->get();
And if you want to select col from both table. 如果你想从两个表中选择col。
DB::table('resurse')
->rightJoin('alocare', 'resurse.id', '=', 'alocare.resursa')
->select('resurse.*','alocare.*')
->groupBy('resurse.id')
->get();
And also if you want to convert your result into array. 如果你想将你的结果转换成数组。
DB::table('resurse')
->rightJoin('alocare', 'resurse.id', '=', 'alocare.resursa')
->select('resurse.*','alocare.*')
->groupBy('resurse.id')
->get()->toArray();
I'm using right join because you also need data from alocare table. 我正在使用右连接,因为您还需要来自alocare表的数据。
If still something not meeting your desired, let me know. 如果仍然无法满足您的期望,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.