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
– pc_oc
The name of this model is right ? Ewempresa, you put in Belongs.
– Diego Souza
@pc_oc does so:
Auth::user()->empresa()->morada_fiscal
please? (check for typoEwempresa::class
). and checkreturn
– novic
In addition, the
return
in relationship function.– Diego Souza
@Zooboomafoo, my model Ewempresa is correct. In the session nothing of Ewempresa is put
– pc_oc
What version of Laravel ?
– Diego Souza
@Zooboomafoo is the 4.2
– pc_oc