Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

danlopes's avatar

Search in related tables

Hey, im creating an property sales site with laravel, and im stuck in Search ,for example user can search by number of bedrooms, type of the house, price, etc...

Probably i make a mistake when i draw database, so my models

Property model - Property can be a house, an apartment, shop , etc....

public function atributos(){

        //se for moradia
        if ($this->tipoimovel_id == 1) {
            return $this->hasOne(Moradia::class);
        }
        //se for apartamento
        else if ($this->tipoimovel_id == 2) {            
            return $this->hasOne(Apartamento::class);
        }
        //se for loja
        else if ($this->tipoimovel_id == 3) {
            return $this->hasOne(Loja::class);
        }
        //se for armazem
        else if ($this->tipoimovel_id == 4) {
            return $this->hasOne(Armazem::class);
        }
        //se for terreno para construção
        else if ($this->tipoimovel_id == 5) {
            return $this->hasOne(TerrenoConstrucao::class);
        }
        // se for terreno para outros fins
        else if ($this->tipoimovel_id == 6) {
            return $this->hasOne(TerrenoOutrosFins::class);
        }
    }       

House model

    public function imovel(){
        return $this->belongsTo(Imovel::class);
    }

Apartment model

    public function imovel(){
        return $this->belongsTo(Imovel::class);
    }

House table

 Schema::create('moradias', function (Blueprint $table) {
            $table->integer('imovel_id');
            $table->integer('nrPisosConstrucao');
            $table->integer('nrQuartos');
            $table->integer('nrWcs');
            $table->integer('areaConstrucao');
            $table->integer('areaTerreno');
            $table->smallInteger('anoConstrucao');
            $table->timestamps();

Apartment table

 Schema::create('apartamentos', function (Blueprint $table) {
            $table->integer('imovel_id');
            $table->integer('nrQuartos');
            $table->integer('nrWcs');
            $table->integer('nrPisosEdifio');            
            $table->integer('areasAcessorias');
            $table->integer('areasHabitacionais');
            $table->smallInteger('anoConstrucao');
            $table->timestamps();

For type of the house and price, i have already do the search and it works, but number of rooms i have a big problem, because i have to search in house table or apartment table. im using scopes to do that

Search in House tables

    public function scopeProcuraNrQuartosMoradia($query,$nrQuartos){
        if ($nrQuartos != 0)  $query ->join('moradias', 'moradias.imovel_id', '=', 'imoveis.id')
                                        ->where('moradias.nrQuartos', '>=', $nrQuartos);
        
     
}

Search in Apartment table

    public function scopeProcuraNrQuartosApartamento($query,$nrQuartos){
        if ($nrQuartos != 0) $query ->join('apartamentos','imoveis.id', '=', 'apartamentos.imovel_id')
                                    ->orWhere( 'nrQuartos' , '>=' , $nrQuartos);      
       }

Of course, i get an error cause one property just can be a house or an apartment, can not be both....

Controller

  $imo = Imovel::procuraFinalidade($finalidade)->procuraTipo($tipo)->procuraNrQuartosMoradia($nrQuartos)->procuraNrQuartosApartamento($nrQuartos)->procuraPreco($minPreco,$maxPreco)->paginate(9);

If i use just one of the scopes, of course works...

My difficult: For example: i want to search an property with 3 number of rooms, i cant do that...,

Thanks

0 likes
0 replies

Please or to participate in this conversation.