How to access foreign key table in Laravel to return in a View

Asked

Viewed 121 times

-1

I am working on the project in the Flat and I can not solve the following problem: I have a Call List that’s being shown like this:
Amostra de como esta a lista de atendimento

I wanted to put the name in place of the code in the columns. However the name is in another table, I do not know how to access. Código da tabela

I’ve already received the foreign key Models in my controller, as you can see I already have access to the code, but I don’t know how to name their place, any idea?

2 answers

0

I can’t visualize, as you are performing the query to search for these elections, probably if you make an Inner Join between these tables can be redeemed. Example:

select * from atendimento 
inner join "aqui vai o nome da tabela" on atendimento.atendimento_id = "nome da tabela"."nome da coluna"; 

Then try to redeem the values in foreach.

0


La in your service model Oce need to define the relationship for example

public function pessoa()
{
    return $this->belongsTo('Model\Pessoa', 'foreign_key_id');
}

there in your view inside the foreach Voce can access so:

@foreach($atendimentos as $atendimentoC)
{{ $atencimentoC->pessoa->nome }}
@endforeach

see the documentation of the Laravel https://laravel.com/docs/5.8/eloquent-relationships

  • 1

    Thank you just lacked the relationship!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.