Php Laravel Belongasmany只回了一张唱片?
我有3个表,我将尝试描述模式Php Laravel Belongasmany只回了一张唱片?,php,laravel,Php,Laravel,我有3个表,我将尝试描述模式 scraper_profiles id name scraper_collections id name scraper_collection_entries profile_id collection_id 我正试图退回属于收藏的物品 class ScraperCollection extends Model { public function items() { return
scraper_profiles
id
name
scraper_collections
id
name
scraper_collection_entries
profile_id
collection_id
我正试图退回属于收藏的物品
class ScraperCollection extends Model
{
public function items()
{
return $this->belongsToMany(ScraperProfile::class, 'scraper_collection_entries', 'profile_id', 'collection_id');
}
}
虽然它在我的资源中只返回一条记录
class CollectionResource extends JsonResource
{
public function toArray($request)
{
return [
'name' => $this->name,
'creator' => new UserResource($this->creator),
'items' => ScraperProfileResource::collection($this->items),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}
外键的多对多定义是相反的 应该是:
return $this->belongsToMany(
ScraperProfile::class,
'scraper_collection_entries',
'collection_id',
'profile_id');
在多对多关系的定义中,有一点需要记住:
如果第一个参数是classA,那么定义的最后一个参数应该是A(
A\u id
)的外键。外键的多对多定义是相反的
应该是:
return $this->belongsToMany(
ScraperProfile::class,
'scraper_collection_entries',
'collection_id',
'profile_id');
在多对多关系的定义中,有一点需要记住:
如果第一个参数是classA,那么定义的最后一个参数应该是A(
A\u id
)的外键。您只接收集合还是单个模型?您只接收集合还是单个模型?