将此SQL语句转换为Laravel Eloquent Relations

时间:2016-10-27 08:25:39

标签: sql laravel eloquent

我有2张桌子

table1
empid
lname
fname


table2
empid
name
department

这是我的SQL声明

Select table1.*, table2.department from table1 inner join table2 on table1.empid = table2.empid

1 个答案:

答案 0 :(得分:0)

例如:

table1
id
lname
fname

table2
id
table1_id
name
department

table2.php模型

class Table2 extends Model
{
protected $primaryKey = 'id';

function withTable1() {
   return $this->hasOne('App\Table1', 'id', 'table1_id');
}

public function show($id){
     Table2::with('withTable1')->where('table1_id', $id)->get();
}

}

$ id是url的参数