Add variables to Auth 4.2

Asked

Viewed 310 times

3

I have my Auth session, which returns user data:

print_r(Auth::user());

In this section, I want to add the company data associated with the user. In my model User, added the following:

public function empresa()
{
    $this->belongsTo(Ewempresa::class,'empresa_id');
}

To fetch company data, in this case the field "morada_fiscal", have this:

echo Auth::user()->empresa->morada_fiscal;

What happens is that the company is not placed in the Auth session.

My model User:

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    public function siteuserlangs()
    {
        return $this->hasMany('Siteuserlang');
    }

    public function siteusertags()
    {
        return $this->hasMany('Siteusertag');
    }

    public function validate($input)
    {
        $rules=array(
            'uniqueid' => 'required',
            //'username' => 'required|email',
            'active' => 'numeric',
            'password' => 'required|min:6',
            'email' => 'required|email',
            //'locale' => 'required',
            //'name' => 'required|min:6',
            );

        return Validator::make($input,$rules);
    }

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'siteusers';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password');

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->email;
    }




    public function empresa()
    {
        return $this->belongsTo(Ewempresa::class,'empresa_id');
    }


    public function getRememberToken()
    {
        return $this->remember_token;
    }

    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    public function getRememberTokenName()
    {
        return 'remember_token';
    }

}

?>

I’m basing myself on the following: https://stackoverflow.com/questions/37210848/where-to-add-auth-session-variables-when-the-user-is-logging-in

  • Yes, to access the company’s "tax address" field, I’m using what you wrote

  • The name of this model is right ? Ewempresa, you put in Belongs.

  • 1

    @pc_oc does so: Auth::user()->empresa()->morada_fiscal please? (check for typo Ewempresa::class). and check return

  • In addition, the return in relationship function.

  • @Zooboomafoo, my model Ewempresa is correct. In the session nothing of Ewempresa is put

  • What version of Laravel ?

  • @Zooboomafoo is the 4.2

Show 2 more comments

3 answers

3


1

Missed the return.

public function empresa()
{
    return $this->belongsTo('Ewempresa', 'empresa_id');
}

echo Auth::user()->empresa->morada_fiscal;
  • Ewempresa is a model and not a class, it then does not find this class "Class 'App Ewempresa' not found "

  • If it’s 4.2, then take out the "App".

  • but if you do print_r(Auth::user()); no Ewempresa data appears, they should not appear?

  • Not. print_r(Auth::user()->empresa) should show up.

  • if you do this it returns the empty array. When I log in, this should not be placed right in the Auth session?

  • Have you checked the Company ID ?

  • yes, they are related

Show 3 more comments

0

Do something like this, follow the controller:

public function dadosUsuario(Request $request)
    {
        $dadosUsuario = Auth::user();
        $dadosUsuario->vendedor = Auth::user()->vendedor;
        return $dadosUsuario;
    }

And the user model is :

protected $table = 'users';
    protected $fillable = ['username', 'name', 'email', 'password', 'id_vendedor', 'admin'];

public function vendedor()
    {
        return $this->belongsTo(Vendedor::class,'id_vendedor');
    }

Browser other questions tagged

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