staudenmeir / belongs-to-through

Laravel Eloquent BelongsToThrough relationships
MIT License
1.17k stars 91 forks source link

How can I change the name of the keys? #21

Closed andreshg112 closed 7 years ago

andreshg112 commented 7 years ago

I need to do something like this:

public function sede() {
        return $this->belongsToThrough(Sede::class, Vendedor::class, [], 'vendedor_id');
    }

But when I do it, I got this:

QueryException in Connection.php line 729: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pedidos.vendedor_idvendedore_id' in 'on clause' (SQL: selectsedes.*,pedidos.idas__deep_related_through_keyfromsedesleft joinvendedoresonsedes.id=vendedores.sede_idleft joinpedidosonvendedores.id=pedidos.vendedor_idvendedore_idwherepedidos.idin (2997, 2998) andsedes.deleted_atis null)

When I do it without the key and array: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pedidos.vendedore_id' in 'on clause' (SQL: selectsedes.*,pedidos.idas__deep_related_through_keyfromsedesleft joinvendedoresonsedes.id=vendedores.sede_idleft joinpedidosonvendedores.id=pedidos.vendedore_idwherepedidos.idin (2997, 2998) andsedes.deleted_atis null)

I have a problem with it because you're using Str::singular. This is just for english words. How can I change the name of my foreign key? Note: The plural for 'vendedor' is 'vendedores', in spanish.

znck commented 7 years ago
 return $this->belongsToThrough(Sede::class, [ ['vendedor_id', Vendedor::class] ]);
andreshg112 commented 7 years ago

Doing this:

public function sede() { return $this->belongsToThrough(Sede::class, [ ['vendedor_id', Vendedor::class] ]); }

I'm getting this:

`FatalErrorException in BelongsToThrough.php line 40: Class 'vendedor_id' not found

in BelongsToThrough.php line 40

`

znck commented 7 years ago

Oh! My bad.

return $this->belongsToThrough(Sede::class, [ [Vendedor::class, 'vendedor_id'] ]);

There is an example in tests. I guess this needs to be documented.