topclaudy / compoships

Multi-columns relationships for Laravel's Eloquent ORM
MIT License
1.12k stars 130 forks source link

Latest update (2.1.3) does not seem to allow DB:raw() for me as examples have shown #139

Open bmcn99 opened 2 years ago

bmcn99 commented 2 years ago

My current relationship using compoships works well: return $this->belongsTo('App\Models\PizzaPrice', ['pizza_id', 'size_id'], ['pizza_id', 'size_id']);

but after updating to 2.1.3 and altering it to use: return $this->belongsTo('App\Models\PizzaPrice', ['pizza_id', DB::raw('size_id')], ['pizza_id', 'size_id']); or return $this->belongsTo('App\Models\PizzaPrice', ['pizza_id', DB::raw('table_name.size_id')], ['pizza_id', 'size_id']);

in order to test the new functionality it produces the following error: array_key_exists(): The first argument should be either a string or an integer

Am I not using this correctly? My end goal would be a relationship that is a few degrees removed and look something like this:

return $this->hasMany('B',
    ['foreignKey1', DB::raw('F.foreignKey2')], //or possibly providing a const value here
    ['localKey1', 'localKey2']
)
->join('C','C.other_key','=','B.other_key');
->join('D','D.other_key','=','C.other_key');
->join('E','E.other_key','=','D.other_key');
->join('F','F.other_key','=','E.other_key');

But that will not work since it dies when trying to use DB::raw('F.foreignKey2'). Please let me know if I can provide any further information and thank you.

erikn69 commented 2 years ago

https://github.com/topclaudy/compoships/pull/122

Hi, 2.1.3 feature works for hasOne, hasMany, fell free to make a PR for belongsTo Also give the file and the line of the exception, Did you try the inverse?

return $this->belongsTo('App\Models\PizzaPrice', ['pizza_id', 'size_id'], ['pizza_id', DB::raw('table_name.size_id')]);

Also try this https://github.com/erikn69/compoships/commit/005a39cdaf8de9a4d198ed56b4ef427ad8f0a116