2
Currently I have the following situation:
- 1 User can belong to several municipalities
- Within that relationship -
municipios_usuarios
- I also need to save who made this relationship, ieusuario_id
public function municipios()
{
return $this->belongsToMany('Projeto\DB\Municipio', 'municipios_usuarios')
->withPivot('criado_por_id', 'atualizado_por_id');
}
How do I set up in Laravel 5.1 the relationship of this new field so that I can access it this way (or any other):
@foreach ($usuario->municipios => $municipios)
{!! $municipio->pivot->criadoPor->nome !!}
@endforeach
The only way I could do that was by making a join()
manually, however I would like to use the Eloquent.
Most of all, thank you.
If you need more information, let me know.
I’m running out of time to assemble the answer, but I believe you’ll have to do something using this: http://laravel.com/docs/5.1/eloquent-relationships#has-Many-through
– gmsantos
@gmsantos I checked out.
– Patrick Maciel
In this case hasManyThrough does not work.
– Wallace Maxters
A gambit I always do is: Without having an n:n table with other relationships, I create a middle table model.
– Wallace Maxters