Get Laravel login user address

Asked

Viewed 1,368 times

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

  • I just added

3 answers

1


When creating this function

public function endereco ()
{
    return $this -> hasOne(Endereco::class,'user_id','id');
}

your model User is replaced by a reference to the model Endereco.

To access this address, just do so {{Auth::user()->endereco}} and from that address search the field you need.

{{Auth::user()->endereco->logradouro}}
{{Auth::user()->endereco->bairro}}
{{Auth::user()->endereco->numero}}
{{Auth::user()->endereco->cidade}}
{{Auth::user()->endereco->uf}}
{{Auth::user()->endereco->cep}}
  • It worked perfectly! Thank you

0

This way you can access the information linked to the logged in user.

$user = Auth::user();

$result = Endereco::where('user_id', '=', $user-id)->get();

$result will return the columns you need.

  • Yes, ok and how do I get the address? It’s two tables, User and Address. I log into the system and I can get the name, but I wanted to get the address too?

  • I edited it, see if that’s what you need.

  • 1

    Excellent tip! I will study !

0

In that question Save Relationship 1:1 on Laravel 5.3 already have a basic example of how to access the relationship and bring the data, maybe it is not the best way as you did {{ Auth::user()->??? }} because it limits itself to always having to do this for the other fields, so in its controller or in a View:: or in a View::share, do:

// informações de user logado pelo sistema do laravel
$user = Auth::user(); 
// todas as informações do endereço do usuário logado
$endereco = $user->endereco()->first(); 

and pass to your View these variables.

Reading:

Browser other questions tagged

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