staudenmeir/eloquent-json-relations

laravel compoships

Closed this issue · 4 comments

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

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

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

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);
    }
}

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