Limit relation by common attribute

Asked

Viewed 35 times

0

I am working on Laravel and I have 3 tables (users, administrations and immobles).

I would like to know, what would be the best way to relate (associate) users and properties that belong to the same administration ?

What would be the best way to separate them by administration, by a middleware and routes, with a simple function? That is, how do I for users who have the same admin id see only the properties that also have this admin id ?

Below is the structure of my models and their current relationships:

Model Administração:

<?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 Imovel:

<?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');
    }
}

inserir a descrição da imagem aqui

  • Read this: https://answall.com/questions/148776/para-que-serve-um-scope-no-laravel/151866#151866 I believe this is what you need?

  • Hello, thanks for the indication, I gave a read there and I believe that does not satisfy my case, because I need to limit the access of users by administration, as well as for real estate

  • Your question suggests this behavior, where in SQL already decide who is the user and the immovable, is now very wide I myself particularly did not understand

  • All users are managing all the properties that I register, and it should not be like this, they should only manage the administration’s properties to which they belong. I confess that the question is quite broad yes, so I myself can not find the best way to implement this separation.

  • All users managing things that does not belong! first question in the tables of immovables has the administration id and the user has how to know which is its administration? or even some table that managed the two information?

  • the immovable table has the administration id and the user also has the administration id to which it is affected

  • Your relationships seem to me wrong, make the following available the diagram of the tables involved! because if you have to do two things list properties per user/administration and create a middleware to block also the data that can be typed for example in the url and access immovable that does not belong your problem is wide

  • For now I have no way to release it as soon as I can send it. But briefly it goes like this: these users are actually employees of the various administrations, and they are who register, edit and eliminate real estate. They cannot manage property that is not owned by their administrations. So basically the relationships I created were: 1 user belongs to an administration, the administration in turn has N users. And 1 property belongs to an administration, the same has N real estate

  • Correct, but, there is no way to help if your question does not contain more details. Ok!

  • I ended up adding the whole diagram but the focus is on the same restrictions part

  • Really the problem is wide, has relations with municipalities, sorry but, this is a big doubt ...!

  • Okay. I know that, but my question is about the user restrictions part, because the rest is already working. Thanks in advance for the effort @Virgilio Novic

Show 7 more comments
No answers

Browser other questions tagged

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