具有两个ID的同一表中的多级关系

时间:2018-03-14 08:44:13

标签: laravel laravel-5.5

我有一个名为tbl_location的表格,其中包含字段id,type,fk_id,name

现在输入此处有值 0 = country 1 = state 2 = city 3 = area

现在,区域记录的城市记录ID为fk_id

城市记录的状态为fk_id

然后州将国家的ID称为fk_id

国家/地区将以0 / null作为fk_id。

现在我想知道如何加入记录来获取区域,城市,州和国家。

更新

根据我需要添加城市,州或国家/地区的类型值。即如果类型是3添加城市,州和国家。 或者如果它是2添加州和国家 或者如果它是1个添加国家。

我的模特

<?php

namespace App\BackendModel;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Location extends Model
{
    use SoftDeletes;

    const ACTIVATE = 1;
    const DEACTIVATE = 0;

    CONST COUNTRY_TYPE = 0;
    CONST STATE_TYPE = 1;
    CONST CITY_TYPE = 2;
    CONST AREA_TYPE = 3;

    protected $dates = ['deleted_at'];
    protected $primaryKey = 'id';
    protected $table = 'tbl_location';

    public function country(){
        return $this->hasOne('App\BackendModel\Location', 'id','fk_id')
        ->with('state')
        ->select('id', 'vchr_name','fk_id', 'int_type')
        ->where('int_type', COUNTRY_TYPE);
    }

    public function state(){
        return $this->hasOne('App\BackendModel\Location', 'id','fk_id')
        ->with('city')
        ->select('id', 'vchr_name','fk_id', 'int_type')
        ->where('int_type', STATE_TYPE);
    }

    public function city(){
        return $this->hasOne('App\BackendModel\Location', 'id','fk_id')
        ->select('id', 'vchr_name','fk_id', 'int_type')
        ->where('int_type', CITY_TYPE);
    }
}

2 个答案:

答案 0 :(得分:0)

如果您正确设置了您的关系,

Location::with('country', 'state', 'city')->get();应该能够胜任

答案 1 :(得分:0)

在等待之后,我想出了我的问题的解决方案,但我认为它可能会更好。

<强>控制器

$location = Location::where('pk_int_location_id', $id)->pluck('int_type');
    switch ($location[0]) {
        case '3':
            $location = Location::with('city')->select('pk_int_location_id as area_id', 'vchr_name as area_name','fk_int_location_id', 'int_type')->where('pk_int_location_id', $id)->first();
            break;
        case '2':
            $location = Location::with('state')->select('pk_int_location_id as city_id', 'vchr_name as city_name','fk_int_location_id', 'int_type')->where('pk_int_location_id', $id)->first();
            break;
        case '1':
            $location = Location::with('country')->select('pk_int_location_id as state_id', 'vchr_name as state_name','fk_int_location_id', 'int_type')->where('pk_int_location_id', $id)->first();
            break;
        case '0':
            $location = Location::select('pk_int_location_id as country_id', 'vchr_name as country_name','fk_int_location_id', 'int_type')->where('pk_int_location_id', $id)->first();
            break;

        default:
            $location = Location::with('city', 'state', 'country')->where('pk_int_location_id', $id)->first();
            break;
    }

<强>模型

public function country(){
    return $this->hasOne('App\BackendModel\Location', 'pk_int_location_id','fk_int_location_id')
    ->select('pk_int_location_id', 'vchr_name as country_name','fk_int_location_id', 'int_type')
    ->where('int_type', Location::COUNTRY_TYPE);
}

public function state(){
    return $this->hasOne('App\BackendModel\Location', 'pk_int_location_id','fk_int_location_id')
    ->with('country')
    ->select('pk_int_location_id', 'vchr_name as state_name','fk_int_location_id', 'int_type')
    ->where('int_type', Location::STATE_TYPE);
}

public function city(){
    return $this->hasOne('App\BackendModel\Location', 'pk_int_location_id','fk_int_location_id')
    ->with('state')
    ->select('pk_int_location_id', 'vchr_name as city_name','fk_int_location_id', 'int_type')
    ->where('int_type', Location::CITY_TYPE);
}