How to use the hasmany relationship in laravel 5.2?

0

I have 3 tables of funcionário , apontamentos and horastrabalhadas .

My relationship is hasmany 1 employee has multiple apontamentos . When I show the result with a dd() , the employee data comes twice and the horastrabalhadas is blank. It has to invert from hasMany to belongTo to see if my logic was wrong but came the same thing.

notes

  

CREATE TABLE IF NOT EXISTS Notes (id int (10) unsigned NOT NULL   AUTO_INCREMENT, varchar pointing (255) COLLATE utf8_unicode_ci NOT   NULL, created_at timestamp NULL DEFAULT NULL, updated_at timestamp   NULL DEFAULT NULL, PRIMARY KEY (id)) ENGINE = InnoDB DEFAULT   CHARSET = utf8 COLLATE = utf8_unicode_ci AUTO_INCREMENT = 4;

teamwork

  

CREATE TABLE IF NOT TECHNICAL EXISTS (id int (10) unsigned NOT   NULL AUTO_INCREMENT, matricula int (10) unsigned NOT NULL, name   varchar (150) COLLATE utf8_unicode_ci NOT NULL, position varchar (150)   COLLATE utf8_unicode_ci DEFAULT NULL, loadhoraria int (10) unsigned   NOT NULL, name team varchar (150) COLLATE utf8_unicode_ci DEFAULT   NULL, created_at timestamp NULL DEFAULT NULL, updated_at timestamp   NULL DEFAULT NULL, PRIMARY KEY (id)) ENGINE = InnoDB DEFAULT   CHARSET = utf8 COLLATE = utf8_unicode_ci AUTO_INCREMENT = 3;

hhs

  

CREATE TABLE IF NOT EXISTS hhs (id int (10) unsigned NOT NULL   AUTO_INCREMENT, idequipe int (10) unsigned NOT NULL, idaption   int (10) unsigned NOT NULL, datename datetime NOT NULL,   timeaptionrequest time NOT NULL, timeappointmentinthe time NOT NULL,   numeroos int (11) NOT NULL, created_at timestamp NULL DEFAULT NULL,   updated_at timestamp NULL DEFAULT NULL, PRIMARY KEY (id), KEY   hhs_idequipe_foreign (idequipe), KEY hhs_idapontamento_foreign   (idapontamento)) ENGINE = InnoDB DEFAULT CHARSET = utf8   COLLATE = utf8_unicode_ci

Has anything like this ever happened to anyone?

    
asked by anonymous 28.12.2016 / 18:34

1 answer

2

Relationship 1: N Laravel

Relationships:

  

Apontamentos

class Apontamentos extends Model
{
    protected $fillable = array('apontamento');
    protected $table = 'apontamentos';
    public $timestamps = true;
    protected $primaryKey = 'id';

    public function hhs()
    {
        return $this->hasMany(Hhs::class, 'idapontamento', 'id');
    }
}
  

Equipetecnicas

class Equipetecnicas extends Model
{
    protected $fillable = array('matricula','nome','cargo','cargahoraria','nomeequipe');
    protected $table = 'equipetecnicas';
    public $timestamps = true;
    protected $primaryKey = 'id';    

    public function hhs()
    {
        return $this->hasMany(Hhs::class, 'idequipe', 'id');
    }
}
  

Hhs

class Hhs extends Model
{
    protected $fillable = array('idequipe','idapontamento','dataapontamento',
            'horaapontamentoinicio','horaapontamentofim','numeroos');
    protected $table = 'hhs';
    public $timestamps = true;
    protected $primaryKey = 'id'; 

    public function apontamento()
    {
        return $this->belongsTo(Apontamentos::class, 'idapontamento', 'id');
    }

    public function equipetecnicas()
    {
        return $this->belongsTo(Equipetecnicas::class, 'idequipe', 'id');
    }
}

Calling Relationships:

Use with to load relationships

Example 1 - Equipetecnicas

public function show($id)
{
    $ep = new App\Models\Equipetecnicas();
    $result = $ep->with('hhs.apontamento')
               ->where('id',$id)
               ->first();
}
  

Output:

App\Models\Equipetecnicas {#740
 id: 1,
 matricula: 1,
 nome: "Nome 1",
 cargo: "Administrador",
 cargahoraria: 120,
 nomeequipe: "Equipe 1",
 created_at: "2016-12-29 00:00:00",
 updated_at: "2016-12-29 00:00:00",
 hhs: Illuminate\Database\Eloquent\Collection {#760
   all: [
     App\Models\Hhs {#762
       id: 1,
       idequipe: 1,
       idapontamento: 1,
       dataapontamento: "2016-12-29 00:00:00",
       horaapontamentoinicio: "13:00:00",
       horaapontamentofim: "14:00:00",
       numeroos: 1,
       created_at: "2016-12-29 00:00:00",
       updated_at: "2016-12-29 00:00:00",
       apontamento: App\Models\Apontamentos {#767
         id: 1,
         apontamento: "Apontamento 1",
         created_at: "2016-12-29 00:00:00",
         updated_at: "2016-12-29 00:00:00",
       },
     },
   ],
 },
}

Example 2 - Hhs

public function show($id)
{
    $hhs = new App\Models\Hhs();
    $result = $hhs->with('apontamento')
                  ->with('equipetecnicas')
                  ->where('id',$id)
                  ->first();
}
  

Exit:

App\Models\Hhs {#746
  id: 2,
  idequipe: 2,
  idapontamento: 2,
  dataapontamento: "2016-12-29 00:00:00",
  horaapontamentoinicio: "15:00:00",
  horaapontamentofim: "16:00:00",
  numeroos: 2,
  created_at: "2016-12-29 00:00:00",
  updated_at: "2016-12-29 00:00:00",
  apontamento: App\Models\Apontamentos {#752
    id: 2,
    apontamento: "Apontamento 2",
    created_at: "2016-12-29 00:00:00",
    updated_at: "2016-12-29 00:00:00",
  },
  equipetecnicas: App\Models\Equipetecnicas {#753
    id: 2,
    matricula: 2,
    nome: "Nome 2",
    cargo: "Gerente",
    cargahoraria: 240,
    nomeequipe: "Equipe 2",
    created_at: "2016-12-29 00:00:00",
   updated_at: "2016-12-29 00:00:00",
 },
}

Must read: Read all that link dealing with Query Builder in constructing SQL from Eloquent

References:

28.12.2016 / 19:07