Laravel - List with list of tables (state/city=>person)

Asked

Viewed 1,551 times

1

I have the following tables:

contacts => id - name - state - city

states => id - status - acronym

cities => id - city - id_state


And the following models:

Php state.

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Estado extends Model{
    protected $table = 'estados';
    public $timestamps = false;

    public function contatos(){
        return $this->hasMany('App\Contato');
    }
}

Contact.php

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Contato extends Model{
    protected $table = 'contatos';
    public $timestamps = false;

    public function estados(){
        return $this->hasMany('App\Estado');
    }
}

And to list:

$request = Contato::orderBy($column, $sort)->paginate($paginator);

And how do I make, instead of the status id, appear the name of the state(the same goes for the neighborhood)

1 answer

2


Good, come on. First let’s impose some doubts as to its logic and if possible some corrections.

Contato hasMany Estados

In free translation this saying: Contact contains several States.

That is correct?

It would no longer be concrete: Contact has a city or Contact belongs to a state, because in question of integrity of its application it is not possible to exist a contact without informing the state.

So it would be relationship hasOne or belongsTo

Now let’s go to the code.

Each contact you should pull by eloquent your relationships, ie if contact comes Carlos Eduardo you should automatically pull the state from it.

To do this there are two ways in Eloquent.

First

When ordering the contact list you call the relationship

$request = Contato::orderBy($column, $sort)
           ->with('estados', 'bairro') 
           ->paginate($paginator);

This method is named after Eager Loading Multiple Relationships. You can check the documentation.

What does he do? He says to the eloquent: Man, every contact you return to me, you seek the estado and takes the bairro also of contact, beauty?

According to

This method will define in the model that every query it undergoes it should automatically call the relationship. This is different from the ONE method, which you define when you want to call the relationship. In this method it will be in ALL querys.

To use this method just create a propriedade in the contact model

public $with = ['estado'];

After doing any of these methods to recover the state name:

$request = Contato::orderBy($column, $sort)
           ->with('estados', 'bairro') 
           ->paginate($paginator);
foreach(...);
 {{ $contato->bairro->sigla }}
endforeach;

I hope I’ve helped.

  • Thank you for the explanation, but you are making the following mistake: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'estados.contato_id' in 'where clause' (SQL: select * from stateswherestates.contact_id in (1, 5, 9, 13, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21)). Because it is trying to search 'contact_id' within the table 'states'?

  • `$this->hasOne('App Status', 'local_key', 'fk_key); in the model, change by your keys

  • It worked out! Vlw man. I have a lot to learn from Aravel yet.

  • @Kelvymmiranda then mark the answer as correct.

Browser other questions tagged

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