【问题标题】:Laravel- Search for an ID in multiple tablesLaravel - 在多个表中搜索一个 ID
【发布时间】:2017-08-02 13:59:50
【问题描述】:

我正在制作一个房地产销售网站。财产可以是房子、公寓、商店等......

我有这样的关系:

模型://属性

class Imovel extends Model...

public function moradia(){
    return $this->hasOne(Moradia::class);
}

public function apartamento(){
    return $this->hasOne(Apartamento::class);
}

//公寓

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

//房子

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

迁移:

//属性

Schema::create('imoveis', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('tipoImovel_id');
            $table->string('finalidade',20);
            $table->string('titulo',100);
            $table->date('data')->nullable();
            $table->integer('concelho_id');
            $table->string('freguesia',50);
            $table->string('rua',150);
            $table->decimal('long', 10, 7)->nullable();
            $table->decimal('lat', 10, 7)->nullable();
            $table->boolean('destaque');
            $table->boolean('estado')->default(true);
            $table->string('descricao',500);
            $table->string('preco',40);
            $table->integer('empresa_id')->default(1);
            $table->timestamps();

//房子

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

//公寓

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

我的问题是:我如何确定该物业是房屋、公寓还是商店......?

我有所有属性的列表,要怎么编辑?我怎么知道它是什么类型的财产?

谢谢

【问题讨论】:

    标签: php mysql laravel join eloquent


    【解决方案1】:

    检查房屋或公寓表中是否存在相关记录。因此,例如,如果 house 表中没有相关记录,则 $impovel->moradia() 将为空。

    例如:

    $imovel = App\Imovel::find($id);
    
    if(!is_null($imovel->moradia())) {
        // Property is a house
    }
    

    编辑:

    我认为你最好在属性表中有列 -> property_type。这可以取值 'house'、'apartment'、'shop' 等。

    这将有助于轻松地仅选择房屋属性/公寓属性。

    除了你可以有一种关系方法:

    public function attributes(){
        if($this->type == 'house') {
            return $this->hasOne(Moradia::class);
        }
    
        if($this->type == 'apartments') {
            return $this->hasOne(Apartamento::class);
        }
    }
    

    而你可以这样做:$imovel->attributes(),这会给出相应的属性属性。

    【讨论】:

    • 我的问题是:这就是link 的样子。单击按钮时如何显示房屋/公寓的详细信息。
    • 是的,您的编辑可能会对我有所帮助!谢谢你,我会试试看,然后我会给你一些反馈。
    • 抱歉,第一次使用 PHP 和 Laravel :S
    • 是的,这行得通。但我还有另一个问题,在视图中如何显示房屋/公寓的细节?
    • 你需要有这个用户界面。您可以在弹出/模态窗口中显示详细信息。
    猜你喜欢
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 2020-03-20
    • 2018-09-25
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    相关资源
    最近更新 更多