我想进行相关模型的急切加载。但是,我不想获取整个相关模型,而只想检索它的ID。所以最后,我会有如下内容:laravel 4急于加载的ID列表
{
"error": false,
"invoices": {
"id": 5,
"biz_id": 7,
"doc_num": 0,
"type": 1,
"due_date": "0000-00-00",
"status": null,
"to": null,
"**related_model**": [1,2,3,4]
}
我宁愿避免循环。
UPDATE
我的理解没有循环我不能做到,我也做了以下内容:
$data = array();
//get models
$models = IncomeDoc::with('relatedReceipts')
->where('type', '!=', 2)
->get()
->toArray();
foreach ($models as $model)
{
$model['related_receipts'] = array_pluck($model['related_receipts'], 'id');
$data[] = $model;
}
现在,这里是我的问题:有没有什么办法,我可以做模型本身的数据操作?此代码不干净,不能重新使用,我宁愿避免它。
通常你不能通过这种方式获得id,但是在获得结果后你必须做更多的事情,并且必须使用“外键”才能获得相关模型。 –