staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels
MIT License
2.67k stars 157 forks source link

issue with composite keys when doing a relation between 2 tables #211

Closed ahmadbenos closed 1 year ago

ahmadbenos commented 1 year ago

I've seen the example in the documentation but it's between 3 tables. I have Cart2s and a Prodbranch table, both have these 2 fields: branchid and prodid. and i want them to match. I'm trying to do such relation in my Cart2s Model like this:

    public function prodbranches(){
        return $this->hasManyDeep(Prodbranch::class, [new CompositeKey('prodid', 'branchid')]);
    }

however i'm getting this error: TypeError: preg_split(): Argument #2 ($subject) must be of type string, Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey given in file xxxx/vendor/staudenmeir/eloquent-has-many-deep/src/HasRelationships.php on line 82

I also tried this:

    public function prodbranches(){
        return $this->hasManyDeep(Prodbranch::class, [new CompositeKey('prodid', 'branchid'), 'id']);
    }

Am i doing something wrong?

staudenmeir commented 1 year ago

Hi @ahmadbenos, HasManyDeep relationships can only work with three tables or more.

It looks like this is the right package for you: https://github.com/topclaudy/compoships

ahmadbenos commented 1 year ago

Okay thank you!