3
Well I tried to read the documentation and develop this relationship but could not, actually I need to inform what is the name of the type of user. The database model is thus :
MODELS
involved
App\User
App\TipoUser
App\UserTipo
And in my model it’s 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();
}
}
MODELO Usertipo
<?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'];
}
TIPOSUSER
<?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 didn’t know how to do
In my html I’m doing so:
<td>{{ $user->tipos->tipos->DscTipo }}</td>