Many Relationship for Many

Asked

Viewed 4,474 times

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 :

inserir a descrição da imagem aqui

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>

1 answer

7


Relations:

When one makes a list many to many in the documentation it is stipulated as follows:

$this->belongsToMany('relacao',  
                     'nome da tabela pivot', 
                     'key ref. model classe atual', 
                     'key ref. outro model classe relação');

in his case there was already an error for not having stipulated the second Paramento with the name of the table that makes the relation. Another thing missing is to inform that you want to bring the column data created_at and deleted_at

Just below the two methods that will be configured in the classes User and TipoUser:

Model:

User:

public function tipos()
{
    return $this->belongsToMany('App\TipoUser','user_tipouser', 'UserId','TipoUserId')
                ->withTimestamps();
}

TipoUser:

public function users()
{
    return $this->belongsToMany('App\User','user_tipouser', 'TipoUserId','UserId')
                ->withTimestamps();
}

Also in relation to your HTML have to check the following now is a list of values as example below:

@foreach($user->tipos as $t)
    <td>{{ $t->NmTipoUser }}</td>
@endforeach

In that link has the explanation in a very practical model observe how it works and make correlation with yours. Other Example link from Stackoverflow itself pt also very good to clear your doubts.

References:

Browser other questions tagged

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