Many relationship to many laravel

3

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>
    
asked by anonymous 04.05.2017 / 01:45

1 answer

5

Relationships:

When making a many-to-many relationship in the documentation is stipulated as follows:

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

In your case there was already an error because you did not stipulate the second side with the name of the table that makes the relation. Another thing that has been missing is to inform you that you want to bring data from columns created_at and deleted_at

Below are the two methods that will be configured in class 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 you have to check the following now is a list of values as an example below:

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

In this link has the explanation in a very practical model observe how it works and make correlation With your. Other link Stackoverflow itself en is also very good to get your questions answered.

References:

04.05.2017 / 03:53