Self relationship in Group table with Laravel and Eloquent?

Asked

Viewed 1,511 times

6

I have a table of grupo with the fields:

id (int not null auto-increment)

Description (sweep)

grupoid (int null)

How would it be:

  • 1) to migration corresponding to that table?
  • 2) the creation of model Eloquent with the relationship?
  • 3) How could you know whether a particular id é Pai, some technique?
  • 4) if the id é Pai how to load the filhos nos relacionamentos?
  • I wanted to understand the negative vote?

1 answer

8


Auto Relationship Laravel

1) the Migration corresponding to that table?

To create the archive (the blank model) in the console do:

php artisan make:migration Grupo

Configuring:

<?php

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

class Grupo extends Migration
{
    public function up()
    {
        Schema::create('grupo', function (Blueprint $table) 
        {    
            $table->increments('id');    
            $table->string('description', 50);    
            $table->integer("grupoid", false, true)
                ->nullable();    
            $table->index("grupoid");    
            $table->foreign("grupoid")
                ->references('id')
                ->on('grupo');                
        });
    }
    public function down()
    {
        Schema::drop('grupo');
    }
}

Generate Table:

php artisan migrate

2) the creation of the Eloquent model with the relationship?

To create the archive (the blank model) in the console do:

php artisan make:model Grupo

Configuring:

namespace App\Models;    
use Illuminate\Database\Eloquent\Model;

class Grupo extends Model
{
    protected $primaryKey = "id";
    protected $fillable = array('description','grupoid');
    protected $table = "grupo";
    public $timestamps = false;

    //relacionamento (auto-relacionamento) item 4 
    public function grupos()
    {
        return $this->hasMany(Grupo::class, 'grupoid', 'id');
    }

    // um técnica do item 3
    public function isFather()
    {
        return is_null($this->attributes['grupoid']);
    }

    // aqui seria o item 3  
    protected $appends = ['is_father'];
    public function getIsFatherAttribute()
    {
        return is_null($this->attributes['grupoid']);
    }
}

3) How could you know if a certain id is Father, some technique?

In the item 2) two techniques were created:

  • One would be to create a method and compare whether the grupoid is null (is_null) and the method was created isFather():

public function isFather()
{
    return is_null($this->attributes['grupoid']);
}
  • But, only with method it does not come out in the results of Array or Json, then we’ll create a appends in that Model Grupo:

protected $appends = ['is_father'];
public function getIsFatherAttribute()
{
    return is_null($this->attributes['grupoid']);
}

resulting in the format JSON:

{"id":4,"description":"Grupo 2","grupoid":null,"is_father":true,"grupos":[]}

4) if id is Father how to carry children in relationships?

Simply with the command with:

$grupo = App\Models\Grupo();
$grupo->with('grupos')->find(4);

Browser other questions tagged

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