Search code examples
laravellaravel-5laravel-5.3

How to extend Auth object in Laravel?


I have a default table users and related table user_details.

When user does auth it retrieves data from users table and is available to global object:

Auth::user()

How to extend Auth::user() with data from related table user_details?

Reason is to show a full information about authenticated user in each pages(at header)


Solution

  • If I understand the question correctly, don't need to extend Auth, just your User model. You can us a couple of different helpers, ->hasOne, ->belongsTo, etc on your User model (assuming you have a user_details model) and eloquent should do the rest.

    https://laravel.com/docs/master/eloquent-relationships

    EDIT: You can add an accessor to your user model. SO, in your app\User.php

    public function getCustomAttribute()
    {
        return 'Custom Attribute';
    }
    

    You should be able to get that anywhere with something like

    Auth::user()->custom
    

    https://laravel.com/docs/master/eloquent-mutators

    (again, it is possible that I am just not understanding your question)