How to return reregistered records between tables in laravel

2

I have 2 city and state tables I own the parana state and would like to know how to return all cities related to Parana state in a simple way without having to make a loop of repetition

Type

$estados = App/Estado::all();
$cidades = App/Cidade::all();
foreach($estado as $estados)
{
    if($estado->id === $cidade->id)
    {
    }
}

I did not test the code but the way I know it would be this somehow simplified way

    
asked by anonymous 20.11.2018 / 12:18

1 answer

2

Let's go to the trivial:

run the following commands:

php artisan make:model Estado -crm

php artisan make:model Cidade -crm

In this way you will make the model, migration and controller in just one command

You will have the following migrations:

  • State
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateEstadosTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('estados', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('nome',500);
            $table->string('sigla',2);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('estados');
    }
}
  • City
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCidadesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cidades', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('estado_id')->unsigned();
            $table->string('nome',500);

            $table->foreign('estado_id')->references('id')->on('estados')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('cidades');
    }
}

This way you have already referenced the foreign key and defined its tables. Run your migration using the php artisan migrate command.

Open the model Cidade and add the following code:

public function Estado()
{
    return $this->belongsTo('App\Estado','estado_id');
}

Open the Estado model and add the following lines:

public function cidades()
{
    return $this->hasMany('App\Cidade','estado_id');
}

Now you just need to use laravel's own tools to do the filters the way you need them:

Use example applied to blade:

@foreach($estados as $estado)
<tr>
    <td class='sua_classe_para_diferenciar'>{{$estado->nome}}</td>
</tr>
    @foreach($estado->cidades as $cidade)
        <tr>
            <td>{{$cidade->nome}}</td>
        </tr>
    @endforeach
@endforeach
    
20.11.2018 / 12:56