Laravel hasManyThrough() 没有返回任何结果
Laravel hasManyThrough() returning no results
我有 3 个 table,我想使用 eloquent
通过另一个加入其中的 2 个
ProductGroup table
--------------------------
| ProductGroupId | Name |
-------------------------
| 1 | Test1 |
| 2 | Test2 |
--------------------------
ProductLine table
-------------------------------------------------
| ProductLineId | ProductGroupId | Name |
------------------------------------------------|
| 1 | 1 | AnotherTest1 |
| 2 | 1 | AnotherTest2 |
-------------------------------------------------
ProductType table
----------------------------------------------
| ProductTypeId | ProductLineId | Name |
---------------------------------------------|
| 1 | 1 | Something1 |
| 2 | 1 | Something2 |
----------------------------------------------
我想加入 ProductType 的 ProductGroup
我尝试使用它作为我的 ProductGroup 模型(不确定我是否正确地完成了 hasManyThrough())
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ProductGroup extends Model
{
protected $primaryKey = 'ProductGroupId';
public $timestamps = false;
public function producttype()
{
return $this->hasManyThrough('App\ProductGroup', 'App\ProductLine', 'ProductGroupId', 'ProductGroupId');
}
}
我想加入来自 ProductGroup table 的特定 id 上的 2 table,所以在 SQL 中确实是
SELECT * FROM ProductType pt
INNER JOIN ProductLine pl ON
pt.ProductLineId = pl.ProductLineId
INNER JOIN ProductGroup pg ON
pg.ProductGroupId = pl.ProductGroupId
WHERE pg.ProductGroupId = 3
我试过了,但没有结果
我可以在查询生成器中执行此操作,但如果有帮助,我宁愿使用 eloquent
$test = ProductGroup::with('producttype')->whereHas('producttype', function ($query) {
$query->where('ProductGroup.ProductGroupId', 3);
})->get();
改变
public function producttype()
{
return $this->hasManyThrough('App\ProductGroup', 'App\ProductLine',
'ProductGroupId', 'ProductGroupId');
}
至
public function producttype()
{
return $this->hasManyThrough('App\ProductType', 'App\ProductLine', 'ProductGroupId', 'ProductLineId');
}
我有 3 个 table,我想使用 eloquent
通过另一个加入其中的 2 个ProductGroup table
--------------------------
| ProductGroupId | Name |
-------------------------
| 1 | Test1 |
| 2 | Test2 |
--------------------------
ProductLine table
-------------------------------------------------
| ProductLineId | ProductGroupId | Name |
------------------------------------------------|
| 1 | 1 | AnotherTest1 |
| 2 | 1 | AnotherTest2 |
-------------------------------------------------
ProductType table
----------------------------------------------
| ProductTypeId | ProductLineId | Name |
---------------------------------------------|
| 1 | 1 | Something1 |
| 2 | 1 | Something2 |
----------------------------------------------
我想加入 ProductType 的 ProductGroup
我尝试使用它作为我的 ProductGroup 模型(不确定我是否正确地完成了 hasManyThrough())
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ProductGroup extends Model
{
protected $primaryKey = 'ProductGroupId';
public $timestamps = false;
public function producttype()
{
return $this->hasManyThrough('App\ProductGroup', 'App\ProductLine', 'ProductGroupId', 'ProductGroupId');
}
}
我想加入来自 ProductGroup table 的特定 id 上的 2 table,所以在 SQL 中确实是
SELECT * FROM ProductType pt
INNER JOIN ProductLine pl ON
pt.ProductLineId = pl.ProductLineId
INNER JOIN ProductGroup pg ON
pg.ProductGroupId = pl.ProductGroupId
WHERE pg.ProductGroupId = 3
我试过了,但没有结果
我可以在查询生成器中执行此操作,但如果有帮助,我宁愿使用 eloquent
$test = ProductGroup::with('producttype')->whereHas('producttype', function ($query) {
$query->where('ProductGroup.ProductGroupId', 3);
})->get();
改变
public function producttype()
{
return $this->hasManyThrough('App\ProductGroup', 'App\ProductLine',
'ProductGroupId', 'ProductGroupId');
}
至
public function producttype()
{
return $this->hasManyThrough('App\ProductType', 'App\ProductLine', 'ProductGroupId', 'ProductLineId');
}