【发布时间】:2020-07-10 22:05:40
【问题描述】:
我有两张桌子:
goods: id | name
imported_goods: id | good_id | amount
imported_goods 具有来自表 goods 的外键值,而我正在尝试做的是:
显示所有货物记录除了有国外的记录 imported_goods 中的关键值。
示例 (CSV):
商品
1,orange
2,apple
3,bannana
4,mango
进口商品
1,1,20 kg
2,2,40 kg
3,3,60 kg
预期结果:
4,mango
所有其他记录都被丢弃
好模特
class Good extends Model
{
/**
* Get the Imported Goods for this Good.
*/
public function imported_goods()
{
return $this->hasMany('App\Models\Imported_good','good_id');
}
}
Imported_Good Model
class Imported_good extends Model
{
/**
* Get the Good info for this Imported Good.
*/
public function good()
{
return $this->belongsTo('App\Models\Good','good_id');
}
}
【问题讨论】:
-
向我们展示一些示例表数据和预期结果。 (minimal reproducible example)
-
@jarlh 添加了具有预期结果的示例。
-
@Rob Streeting 是的,但它的答案是原始 SQL 查询,在这里我希望它具有雄辩的关系,因为我使用的是 Laravel。 El_vanja 答案很棒,但它并没有真正从 Eloquent Engine 和 Nether 中受益。