0
I have a project on Laravel 6 and I can get the username logged in:
{{ Auth::user()->nome }}
It turns out that this user’s address is in another Onetoone table where 1 user has 1 address.
How do I get this login user address data?
{{ Auth::user()->??? }}
Follow the 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');
}
}
Address:
<?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');
}
}
Put the two models in question
– novic
I just added
– AmandaRJ