How to use hasmany relationship in Laravel 5.2?

Asked

Viewed 5,387 times

1

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

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

Table apontamentos

CREATE TABLE IF NOT EXISTS apontamentos ( id int(10) unsigned NOT NULL
AUTO_INCREMENT, apontamento varchar(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 ;

Table equipetecnicas

CREATE TABLE IF NOT EXISTS equipetecnicas ( id int(10) unsigned NOT
NULL AUTO_INCREMENT, matricula int(10) unsigned NOT NULL, nome
varchar(150) COLLATE utf8_unicode_ci NOT NULL, cargo varchar(150)
COLLATE utf8_unicode_ci DEFAULT NULL, cargahoraria int(10) unsigned
NOT NULL, nomeequipe 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;

Table hss

CREATE TABLE IF NOT EXISTS hhs ( id int(10) unsigned NOT NULL
AUTO_INCREMENT, idequipe int(10) unsigned NOT NULL, idapontamento
int(10) unsigned NOT NULL, dataapontamento datetime NOT NULL,
horaapontamentoinicio time NOT NULL, horaapontamentofim 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

Something similar has happened to someone?

1 answer

6


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:

Utilize with to load the relationships

Example 1 - Equipetecnicas

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

Exit:

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 this link that deals with the Query Builder, in the construction of SQL from the Eloquent

References:

  • I will have to test later and comment here. Urgent demand arose.

  • Virgil, I’m not sure how to do it. Can you tell me what to call this method? I am simply giving a feedback using the show method by selecting a staff member in the hope that they will be shown the hours worked relationships (hh) along with pointing (appointment description).

  • @Marcosbirro I believe that with these two examples I can understand how this loading of the relationships, exists for inner join Yes there is, so I put this link for your reading that has everything you need there. If you have more questions I believe you have to open a new question that in question has been answered your initial question.

  • @Juniorramoty did not understand, if you want maybe open a question!

Browser other questions tagged

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