thiagoprz / eloquent-composite-key

Eloquent Composite Key Support Package
MIT License
44 stars 6 forks source link

ErrorException: Array to string conversion #5

Open suityou01 opened 4 months ago

suityou01 commented 4 months ago

I have successfully installed the package and created a model and factory with a composite key.

However when I create a model that has a "BelongsTo" relationship I get an error such that

ErrorException: Array to string conversion

/www/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:255
/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php:97
/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Relation.php:91
/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php:67
/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php:256
/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php:239
/shared/src/Models/Child.php:62
/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/BelongsToRelationship.php:52
/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:467
/www/vendor/laravel/framework/src/Illuminate/Collections/Arr.php:598
/www/vendor/laravel/framework/src/Illuminate/Collections/Collection.php:777
/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:466
/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:446
/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:453
/www/vendor/laravel/framework/src/Illuminate/Collections/Traits/EnumeratesValues.php:791
/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:448
/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:433
/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:417
/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php:155
/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:416
/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:390
/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:284
/www/tests/Feature/Livewire/Test.php:441
/www/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:61

Line 97 in BelongsTo.php is linkked to here for your convenience.

Also the model definitions are as follows :

Belongs to from the child model is defined as

public function myParent(): BelongsTo
    {
        return $this->belongsTo(MyParent::class, ['column_1',
            'column_2'], ['key_1', 'key_2']
        );
    }

The parent mode is defined as

class ParentModel extends Model { use HasFactory; use HasCompositeKey;

protected $guarded = [];
protected $primaryKey = ['key_1', 'key_2'];
Bruoche commented 4 months ago

Hi,

Had the same issue today, I do not know if this package support relationships with composite keys but if it does I missed it Altho in the meantime I was able to work around it with another package that do specifically relationships with composite keys : https://github.com/tylernathanreed/laravel-composite-relations

'Hope this helps

thiagoprz commented 4 months ago

I will be testing some solution based on extending BelongsTo and the other types of relationship to solve it.