php - Laravel 5.6 API 资源集合 - 未获取条件关系
问题描述
我正在体验我的第一个 Laravel 项目,我实现了一个资源收集 API,我通过护照获取数据。数据似乎从模型中正确检索,关系除外。情况如下:
item.php (模型)
<?php
// Definizione Namespace
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Classe Item
*/
class Item extends Model
{
use SoftDeletes;
// Dichiarazione Proprietà
protected $table = 'item';
protected $dateformat = 'Y-m-d';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'data_acquisto',
'labeled',
'estensione_garanzia',
'stato',
'data_dismissione',
'note'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'codice',
'serial',
'componente_id',
'tipologia_id',
'condizione_id',
'locazione_id',
'fornitore_id',
'parent_id'
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'data_acquisto',
'data_dismissione',
'deleted_at'
];
/**
* All of the relationships to be touched.
*
* @var array
*/
protected $touches = [
'componenti',
'condizioni',
'fornitori',
'locazioni',
'tipologie'
];
/**
* Scope query item figli
* Getter
* @param array $query Query
* @return array Query
*/
public function scopeFigli($query)
{
return $query->where('parent_id', '!=', null);
}
/**
* Componenti Correlati
* Getter
* @return object Componenti
*/
public function componenti()
{
// Definizione relazione
return $this->belongsTo('App\Componente');
}
/**
* Condizioni Correlate
* Getter
* @return object Condizioni
*/
public function condizioni()
{
// Definizione relazione
return $this->belongsTo('App\Condizione');
}
/**
* Fornitori Correlati
* Getter
* @return object Fornitori
*/
public function fornitori()
{
// Definizione relazione
return $this->belongsTo('App\Fornitore');
}
/**
* Locazioni Correlate
* Getter
* @return object Locazioni
*/
public function locazioni()
{
// Definizione relazione
return $this->belongsTo('App\Locazione');
}
/**
* Tipologie Correlate
* Getter
* @return object Tipologie
*/
public function tipologie()
{
// Definizione relazione
return $this->belongsTo('App\Tipologia');
}
}
item.php(资源)
<?php
// Definizione Namespace
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\Componente as ComponenteResource;
use App\Http\Resources\Condizione as CondizioneResource;
use App\Http\Resources\Fornitore as FornitoreResource;
use App\Http\Resources\Locazione as LocazioneResource;
use App\Http\Resources\Tipologia as TipologiaResource;
class Item extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
parent::toArray($request);
return [
'id' => $this->id,
'codice' => $this->codice,
'data_acquisto' => $this->data_acqisto,
'serial' => $this->serial,
'labeled' => $this->labeled,
'estensione_garanzia' => $this->estensione_garanzia,
'stato' => $this->stato,
'data_dismissione' => $this->data_dismissione,
'note' => $this->note,
'parent_id' => $this->parent_id,
// Includi associazioni se caricate
'componenti' => ComponenteResource::collection($this->whenLoaded('componenti')),
'condizioni' => CondizioneResource::collection($this->whenLoaded('condizioni')),
'fornitori' => FornitoreResource::collection($this->whenLoaded('fornitori')),
'locazioni' => LocazioneResource::collection($this->whenLoaded('locazioni')),
'tipologie' => TipologiaResource::collection($this->whenLoaded('tipologie'))
];
}
}
这是有关获取数据示例的屏幕:
如上所示,没有任何关系的痕迹。通过谷歌搜索并按照以下建议更改代码:
// Resoruce - Straight including relations instead of lazy load
[...]
'componenti' => ComponenteResource::collection($this->componenti),
[...]
或通过显式模型中的外键:
/**
* Componenti Correlati
* Getter
* @return object Componenti
*/
public function componenti()
{
// Definizione relazione
return $this->belongsTo('App\Componente', 'componente_id');
}
我仍然没有检索关系。谁能给我一点帮助/提示来解决这个问题?
提前感谢您的帮助。
解决方案
下面的代码将仅在显式加载时显示 Tipologie,以避免 N+1 查询问题。
'tipologie' => TipologiaResource::collection($this->whenLoaded('tipologia'))
要加载 Tipologie for Resource 以显示它,您需要将其显式加载为:
$itemResource = new ItemResource($item->load('tipologia', ... other relationships...);
有关这方面的更多信息,请参阅Eager Loading。
编辑
抱歉不理解关系的类型,就像@luca-cattide 说的那样,collections 不应该用于belongsTo,正确的是使用:
TipologiaResource::make($this->tipologia);
或者还有:
new TipologiaResource($this->topologia);
但是我建议你之前使用“load”方法来加载信息,否则你在数据库中搜索“item”,另一个通过“typologie”等等,直到加载所有的关系。
还有另一种无需加载项目即可加载信息的方法,请参见下文:
new ItemResource(App\Item::find(1)->with(['tipologie', ... other relationships ... ])->get());
在此处查看有关 N+1 查询问题的更多信息。
推荐阅读
- java - Struts 应用程序的 java 安全过滤器
- mysql - 仅当mysql中的所有值都为真时才连接
- c# - 如何使用模板和数据库数据 iText C# 创建模板并生成 pdf
- ios - 将字符串粘贴到 uitextview 中的文本底部时会添加额外的空间
- python - Python 和 Wordpress,缓慢的 XMLRPC 调用
- c# - 从文本文件中仅读取 [Brackets] 字符串
- angular - Angular Lazy Routes 和可选的矩阵参数
- sphinx - Sphinx 如何在 soundex 中使用 wordforms?
- javascript - videojs 函数内的基本实现
- c# - 重绘 ContentControl 的 DataTemplate