简体   繁体   中英

Laravel Mysql JOIN not working

I have 2 tables:

I need to get the table2 rows for each table 1 rows if table2.id=table1.id are equal.

I need something like this: t1.row1=array("t2.row1","t2.row2" etc.);

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.

DB::table('resurse')
->rightJoin('alocare', 'resurse.id', '=', 'alocare.resursa')
->groupBy('resurse.id')
->get();

And if you want to select col from both table.

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.

If still something not meeting your desired, let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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