0
我在加入Laravel中的表格時遇到了實際問題。Laravel 5加入多個表格
我可以加入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
我的所有其他查詢,並加入完美地工作,怎麼樣呼應的原始SQL,所以我可以看到發生了什麼跑? – AdRock