2017-02-09 45 views
1

我有兩個模型。我怎麼可以結合通過ID?像$紀錄─>工會($ datetimesarchive, 'ID1', '=', 'ID2'):Laravel工會兩個模型編號

class Record extends Model 
{ 
    protected $table = 'records'; 

    public function getByVar($var) 
    { 
     $data = Record::where('var', $var) 
      ->select('fio', 'var1', 'var2') 
      ->get(); 
     return $data; 
    } 
} 

class DateTimeArchive extends Model 
{ 
    protected $table = 'table2'; 

    public function getStatus($status) 
    { 
     $data = DateTimeArchive::where('status', $status) 
      ->select('date', 'var1', 'var2') 
      ->get(); 
     return $data; 
    }  
} 

IM在laravel試圖重構這樣:

$result = $mysqli->query(" 
SELECT z.fio, z.var1, z.var2, d.date, d.var1, d.var2 
FROM table1 z, table2 d WHERE (z.var1 = 'mytext' AND d.var2 = 'othertext' AND z.id1 = d.id2)"); 

回答

1

可以使用「合併」 示例:

$result1=DateTimeArchive::where('status', $status) 
     ->select('date', 'var1', 'var2') 
     ->get(); 
    $result2=Record::where('var', $var) 
     ->select('fio', 'var1', 'var2') 
     ->get(); 
$result=$result1->merge($result2);