Well, I've tried to read the documentation and develop this relationship, but I can not really tell you what the user's name is. The database model looks like this:
% of% involved
App\User
App\TipoUser
App\UserTipo
And in my model it looks like this:
MODEL User
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table = 'user';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'NmUser',
'Email',
'Senha',
'CPF',
'Celular',
'DtNasc',
'FlgTipo',
'Rua',
'Bairro',
'Cidade',
'UF',
'CEP',
'Num',
'Comple',
'FlgStatus',
'DtPagamento',
'DtVencimento'
];
protected $primaryKey = 'UserId';
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'Senha'
];
public function tipos()
{
return $this->belongsToMany('App\UserTipo','user_tipouser', 'UserId','TipoUserId')
->withTimestamps();
}
}
UserTipo MODEL
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class UserTipo extends Model
{
use SoftDeletes;
protected $table = 'user_tipouser';
protected $fillable = [
'UserId',
'TipoUserId',
];
protected $dates = ['deleted_at'];
}
MODEL TypesUser
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class TiposUser extends Model
{
protected $table = 'tipouser';
protected $fillable = [
'TipoUserId',
'NmTipoUser',
'DscTipo'
];
public function users()
{
return $this->belongsToMany('App\UserTipo','user_tipouser', 'TipoUserId','UserId')
->withTimestamps();
}
}
My other models could not do it
In my html I'm doing this:
<td>{{ $user->tipos->tipos->DscTipo }}</td>