foreign-keyshas-manycompositelaravel-11

Laravel HasMany through composite key


I have 1:N relation where parent table LOCATIONS contains fields server_id and location_id and table DEVICES is related to the LOCATIONS throught fields server_id and location_id. How can I establish the HasMany() and BelongsTo() between this two tables?

If I use this it throws me an error: "Array to string conversion".

 public function devices(): HasMany
 {
     return $this->hasMany(Device::class, ['server_id', 'location_id'], ['server_id', 'location_id']);
 }

Solution

  • As I found out there is no way to do it in pure Laravel but there is a package which solve this issue https://github.com/topclaudy/compoships