Sql Laravel 5连接多个表
我在加入拉雷维尔的表格时遇到了真正的问题 我可以连接两个表,但只要我尝试连接第三个表,就不会得到任何数据Sql Laravel 5连接多个表,sql,join,laravel-5,Sql,Join,Laravel 5,我在加入拉雷维尔的表格时遇到了真正的问题 我可以连接两个表,但只要我尝试连接第三个表,就不会得到任何数据 class Main extends Model { protected $table = 'main'; protected $primaryKey = 'id'; public function test() { return $this->hasOne('App\Models\Test', 'id', 'id'); }
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运行它时,它就工作了。我想用拉威尔的方式写这个
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
在不运行代码的情况下,您是否尝试过使用('test.rows')?这将获取带有测试行的主实例的测试。我放弃了Eloquent,因为它会导致问题,而是使用了它,这正是我想要的
DB::table('main')
->join('test', 'main.id', '=', 'test.id')
->join('row', function ($join) {
$join->on('main.id', '=', 'row.id')
->whereRaw('main.my_field= row.my_field');
})
->where('somefield', '103288');
我的所有其他查询和连接都工作得很好,那么回显原始SQL以便查看运行的是什么呢?