Add variables to the session Auth laravel 4.2

3

I have my Auth session, which returns the user's data:

print_r(Auth::user());

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

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

To get company data, in this case the field " morada_fiscal ", I 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:

    
asked by anonymous 28.12.2016 / 13:34

3 answers

3

Remarks:

The link you are using as a reference have errors in the code do not use, always use official documentation of post-tag" title="show questions tagged 'laravel'"> laravel

return was missing in method

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

do

echo Auth::user()->empresa()->first()->morada_fiscal
    
28.12.2016 / 13:42
1

return was missing.

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

echo Auth::user()->empresa->morada_fiscal;
    
28.12.2016 / 13:43
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's model is:

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

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