staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

issue with composite keys when doing a relation between 2 tables

ahmadbenos opened this issue · comments

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?

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

Okay thank you!