Last active
April 24, 2017 17:33
-
-
Save ubiratanlima/2333dab9de2e472e7337f0b16c2c9329 to your computer and use it in GitHub Desktop.
Duvidas Laravel
This file contains 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
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Model; | |
class Empresa extends Model | |
{ | |
public function funcionarios() | |
{ | |
return $this->hasMany(Funcionario::class); | |
} | |
} |
This file contains 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
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use App\Models\Empresa; | |
use App\Models\Funcionario; | |
class empresaController extends Controller | |
{ | |
public function funcionarioEmpresa() | |
{ | |
$funcionario_nome = 'Ubirajara'; | |
$funcionario = Funcionario::where('nome', $funcionario_nome)->get()->first(); | |
echo $funcionario->nome."<BR>"; | |
$empresa = $funcionario->empresa; | |
echo "Empresa: {$empresa->razaosocial}"; | |
} | |
} |
This file contains 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
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Model; | |
class Funcionario extends Model | |
{ | |
public function empresa_do_funcionario() | |
{ | |
return $this->belongsTo(Empresa::class); | |
} | |
} |
This file contains 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
Com a ajuda de um amigo, verificamos o erro debugando com dd(Funcionario), ele retornava uma coleção de items e não apenas um item, poderia ser resolvido apenas colocando um ->first() para retornar apenas um registro. | |
$funcionario_nome = 'Ubirajara'; | |
$funcionario = Funcionario::where('nome', $funcionario_nome)->get()->first(); | |
echo $funcionario->nome."<BR>"; | |
$empresa = $funcionario->empresa; | |
echo "Empresa: {$empresa->razaosocial}"; | |
Revisões 2 e 3 funcionando. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment