douren1891 2017-03-31 19:26
浏览 153

Laravel - 如何知道属性是否存在于relacional表中

im doing a property sales website, and i have todo an advanced search, with number of bedroom for example.

but i have properties like shop that dont have bedroom attributes,how to know each table i have to search like apartment or house. i'm using scopes and for location and type of properties works fine.

My property model

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

Warehouse dont have number of bedrooms

Schema::create('armazens', function (Blueprint $table) {
    $table->integer('imovel_id');           
    $table->integer('areaAcessoria');
    $table->integer('areaTotal');
    $table->smallInteger('anoConstrucao');
    $table->timestamps();
});

house have

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();
});

My scope to search by number of bedrooms

public function scopeProcuraNrQuartos($query,$nrQuartos){
    if ($nrQuartos) $query->where( $this->atributos()->nrQuartos , '>=' , $nrQuartos);
} 

i know that its wrong, but exist some validation to know if that attribute exists in the table? Thanks

  • 写回答

1条回答 默认 最新

  • doucepei5298 2017-03-31 22:53
    关注

    IF i got you right, you need to check if attribute (column) exists in database?

    If so, here is how you do it:

    Schema::hasColumn($model->getTable(), $column);
    

    So in your case just put this check in place of

    if ($nrQuartos) 
    

    with your model and columns names of course

    Originally posted here: https://laracasts.com/discuss/channels/eloquent/test-attributescolumns-existence?page=1

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并