【发布时间】:2017-07-10 01:57:15
【问题描述】:
我在 Laravel 中加入表格时遇到了真正的问题。
我可以加入 2 个表,但一旦我尝试加入第三个表,我就没有数据
class Main extends Model
{
protected $table = 'main';
protected $primaryKey = 'id';
public function test()
{
return $this->hasOne('App\Models\Test', 'id', 'id');
}
public function row()
{
return $this->hasOne('App\Models\Row', 'id', 'id');
}
}
class Test extends Model
{
protected $table = 'test';
protected $primaryKey = 'id';
public function main()
{
return $this->belongsTo('App\Models\Main', 'id', 'id');
}
public function rows()
{
return $this->hasMany('App\Models\Row', 'id', 'id');
}
}
class Row extends Model
{
protected $table = 'row';
protected $primaryKey = 'id';
public function main()
{
return $this->belongsTo('App\Models\Main', 'id', 'id');
}
public function rows()
{
return $this->belongsTo('App\Models\Test', 'id', 'id');
}
}
我已将此添加到我的主模型中以运行查询。这将输出 2 个表
public function scopeGetPart($query, somefield)
{
return $query->with('test.main')
->where('somefield', somefield);
}
这在我通过 phpMyAdmin 运行时有效。我想用 Laravel 的方式写这个
SELECT * FROM
main
INNER JOIN test ON
main.id = test.id
INNER JOIN row ON
main.id = row.id
AND main.my_field = row.my_field
WHERE somefield = 103288
【问题讨论】: