I'm working on Laravel and I have 3 tables (users, administrations and real estate).
I would like to know, what would be the best way to relate (associate) users and properties belonging to the same administration?
What would be the best way to separate them by administration, middleware and routes, with a simple function? That is, how do I make users who have the same admin id see only properties that also have this admin id?
Below is the structure of my models and their current relationships:
Model Administration:
<?php
namespace App\Models\Painel;
use Illuminate\Database\Eloquent\Model;
use App\User;
class AdministracaoComunal extends Model
{
protected $table = 'administracoes_comunais';
protected $fillable = [
'nome'
];
public $timestamps = false;
// Relacionamnto OneToMany (os usuarios que estão vinculados a uma administracao)
public function users()
{
return $this->hasMany(User::class);
}
// Relacionamnto OneToMany (os imoveis que estão vinculados a uma administracao)
public function imoveis()
{
return $this->hasMany(Imovel::class, 'administracao_comunal_id');
}
}
Model User:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Models\Painel\AdministracaoComunal;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function administracaoComnunal(){
return $this->belongsTo(AdministracaoComunal::class);
}
public function roles()
{
return $this->belongsToMany(\App\Role::class);
}
public function hasPermission(Permission $permission)
{
return $this->hasAnyRoles($permission->roles);
}
public function hasAnyRoles($roles)
{
// Se tiver mais de um papel cai nesse loop
if (is_array($roles) || is_object($roles)) {
return !! $roles->intersect($this->roles)->count();
}
// Se tiver um papel retorna diretamente aqui
return $this->roles->contains('funcao', $roles);
}
}
Model Property:
<?php namespace App\Models\Painel;
use Illuminate\Database\Eloquent\Model;
use App\User;
class Imovel extends Model
{
protected $table = 'imoveis';
protected $fillable = [
'matricula',
'finalidade',
'tipo_imovel',
'qtd_comodos',
'largura',
'comprimento',
'longitude',
'latitude',
'provincia',
'municipio',
'distrito_comuna',
'bairro',
'rua',
'n_casa',
'numero_escritura',
'data_criacao',
'situacao_licenca',
'situacao_ocupacao',
'valor_imovel',
'valor_aluguel',
'valor_patrimonial',
'administracao_comunal_id',
];
public function administracaoComunal()
{
//
return $this->belongsTo(AdministracaoComunal::class, 'administracao_comunal_id');
}
public function users()
{
//
return $this->belongsToMany(User::class, 'user_imovel');
}
public function proprietarioSingular()
{
//
return $this->belongsToMany(ProprietarioSingular::class, 'imovel_proprietario_singular');
}
public function proprietarioColetivo()
{
//
return $this->belongsToMany(ProprietarioColetivo::class, 'imovel_proprietario_coletivo');
}
}