staudenmeir / eloquent-json-relations

Laravel Eloquent relationships with JSON keys
MIT License
1k stars 63 forks source link

laravel compoships #109

Closed Xlector closed 8 months ago

Xlector commented 8 months ago

Problem: We cannot use this package with laravel compoships

Error:Trait method Staudenmeir\\EloquentJsonRelations\\HasJsonRelationships::getAttribute has not been applied as App\\Models\\Model::getAttribute, because of collision with Awobaz\\Compoships\\Compoships::getAttribute

staudenmeir commented 8 months ago

Hi @Xlector, How are you using or want to use both packages in your model? What relationships did you define or want to define?

Xlector commented 8 months ago

hi @staudenmeir , yes I'm using both of the packages in one model

this using : HasJsonRelationships package public function products(){ return $this->belongsToJson(Product::class,'product_ids'); } this using :Compoships package public function warhouses_per_date(){ return $this->hasMany(Warehouse::class,['date','warehouse_id'],['date','id']); } using them separately works fine

staudenmeir commented 8 months ago

Try combining the packages like this:

class YourModel extends Model
{
    use Compoships, HasJsonRelationships {
        Compoships::getAttribute as getAttributeCompoships;
        HasJsonRelationships::getAttribute as getAttributeJson;
        Compoships::newBelongsTo insteadof HasJsonRelationships;
        Compoships::newHasMany insteadof HasJsonRelationships;
        Compoships::newHasOne insteadof HasJsonRelationships;
    }

    public function getAttribute($key)
    {
        if (is_array($key)) {
            return $this->getAttributeCompoships($key);
        }

        return $this->getAttributeJson($key);
    }
}
Xlector commented 8 months ago

@staudenmeir thanks , I tried It ,It works great 👌.