Created
March 23, 2017 20:25
-
-
Save jjcodes78/157de2c1848fa409c10e47aec8b9963e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//*********************************************** | |
// ESTABELECIMENTO MODEL | |
//*********************************************** | |
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Estabelecimento extends Model | |
{ | |
// | |
public $table = "estabelecimentos"; | |
protected $fillable = ['nmFantasia']; | |
protected $primaryKey = 'idEstabelecimento'; | |
public function tipoEstabelecimento() | |
{ | |
return $this->belongsTo( | |
TipoEstabelecimento::class, | |
'tipoEstabelecimento_idTipoEstabelecimento', | |
'idTipoEstabelecimento' | |
); | |
} | |
} | |
//************************************************** | |
// TIPO ESTABELECIMENTO MODEL | |
//************************************************** | |
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class TipoEstabelecimento extends Model | |
{ | |
// | |
public $table = "tipo_estabelecimentos"; | |
protected $primaryKey = 'idTipoEstabelecimento'; | |
protected $fillable = ['nmTipoEstabelecimento']; | |
public function estabelecimentos() | |
{ | |
return $this->hasMany( | |
Estabelecimento::class, | |
'tipoEstabelecimento_idTipoEstabelecimento', | |
'idTipoEstabelecimento' | |
); | |
} | |
} | |
//****************************************** | |
// A CONSULTA | |
//****************************************** | |
// Considere o tipo 1 cadastrado com o nome 'Tipo A' | |
// USANDO O TINKER | |
Psy Shell v0.8.3 (PHP 7.1.1 — cli) by Justin Hileman | |
>>> $estab = \App\Estabelecimento::first() | |
=> App\Estabelecimento {#671 | |
idEstabelecimento: "1", | |
nmFantasia: "Teste", | |
tipoEstabelecimento_idTipoEstabelecimento: "1", | |
created_at: "1490238000000", | |
updated_at: "1490238000000", | |
} | |
>>> $estab->tipoEstabelecimento | |
=> App\TipoEstabelecimento {#669 | |
idTipoEstabelecimento: "1", | |
nmTipoEstabelecimento: "Tipo A", | |
created_at: "1490238000000", | |
updated_at: "1490238000000", | |
} | |
>>> $estab->tipoEstabelecimento->nmTipoEstabelecimento | |
=> "Tipo A" | |
>>> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment