我有以下表格;
shipment
==========
date document_no address_id
----------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0
item
=======
shipment_date document_no address_id
-------------------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0
下面是模型;
class Shipment extends Model {
protected $table = 'shipment';
public function items()
{
return $this->hasMany(Item::class, 'document_no', 'document_no')
->where(['shipment_date' => $this->date, 'address_id' => $this->address_id]);
}
}
class Item extends Model {
protected $table = 'item';
}
我试图获取API的数据;
return Shipment::with('items')->where('date', $shipment_date)->get()->toArray();
但是我items
空了。但是很奇怪,我在使用items
数据时会得到它。 Shipment::find($id)->items
。我想念什么?
答案 0 :(得分:1)
不能将export class Person {
Id: string;
Name: string;
Age: number;
Selected: boolean;
}
与急切加载结合使用。
答案 1 :(得分:0)
该关系无法正常工作,导致结构不正确。像这样尝试
shipments
==========
id date document_no address_id
----------------------------
1 2018-11-20 SO-18-11971 0
2 2018-11-20 SO-18-11971 1
3 2018-11-21 SO-18-11972 0
item
=======
shipment_id
-------------------------------------
1
2
3
冗余少,效率高。关系将是
class Shipment extends Model {
public function items()
{
return $this->hasMany(Item::class);
}
}
在我的示例中,我将表名从shipment
更改为shipments
,所以您无需声明表或外键。
那说,雄辩的(和Laravel)在关系中不支持多键索引。