Tenho um projeto no Laravel 6 e consigo pegar o nome do usuário logado:
{{ Auth::user()->nome }}
Acontece que o endereço desse usuário está em outra tabela OneToOne onde 1 usuário tem 1 endereço.
Como faço para pegar esse dado de endereço do usuário logado?
{{ Auth::user()->??? }}
Seguem os Models:
User:
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
public function getAuthPassword()
{
return $this->senha;
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'nome', 'email', 'senha',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'senha','remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function endereco ()
{
return $this -> hasOne(Endereco::class,'user_id','id');
}
}
Endereço:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Endereco extends Model
{
protected $table = 'enderecos';
protected $fillable = [
'id', 'user_id', 'logradouro', 'bairro','numero','cidade','uf','cep',
];
public function user()
{
return $this->belongsTo(User::class,'user_id','id');
}
}