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

Multiple level relationship in the same table with two ids

我有一个名为 tbl_location 的 table,字段为 id,type,fk_id,name

现在这里输入值 0 = country 1 = state 2 = city 3 = area

现在区域记录的城市记录id为fk_id

城市记录的州 ID 为 fk_id

然后 state 的国家 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);
    }
}

Location::with('country', 'state', 'city')->get(); 如果你正确地设置了你的关系

应该能胜任这份工作

等待之后,我想到了这个问题的解决方案,但我认为它本来可以更好。

控制器

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