如何反转 Eloquent Has One 和 Has Many Through (laravel 5.8)?

Ahm*_*hel 7 php mysql laravel eloquent

我在下面附上了三个关系表。

https://drive.google.com/file/d/1q1kdURIwFXxHb2MgdRyBkE1e3DMug7r-/view?usp=sharing

我还有三个单独的模型,其中定义了所有表之间的关系。我可以使用hasManyThrough()关系从国家/地区模型中读取城市模型的信息,但无法从城市模型中读取国家/地区信息。我尝试使用“hasManyThrough”检索城市模型,但没有得到结果(附加为注释国家方法)。请阅读我的模型及其关系方法。

有人可以帮助我使用 Eloquent 方法hasManyThrough / hasManyThrough或使用逆来 获取城市模型的信息吗hasManyThrough / hasManyThrough

01.

<?php

namespace App\Hrm;

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

class Country extends Model
{
    //use SoftDeletes;
    protected $fillable = ['name','description','status'];


    public function districts(){
        return $this->hasMany(District::class);
    }


    public function cities(){
        return $this->hasManyThrough(City::class,District::class);
    }


}

Run Code Online (Sandbox Code Playgroud)

02.

<?php

namespace App\Hrm;

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

class District extends Model
{
    //use SoftDeletes;
    protected $fillable = ['country_id','name','description','status'];


    public function country(){
        return $this->belongsTo(Country::class);
    }

    public function cities(){
        return $this->hasMany(City::class);
    }

}
Run Code Online (Sandbox Code Playgroud)

3.

namespace App\Hrm;

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

class City extends Model
{
    //use SoftDeletes;
    protected $fillable = ['district_id','name','description','status'];

    public function district(){
        return $this->belongsTo(District::class);
    }

//    public function country(){
//        return $this->hasOneThrough(Country::class, District::class);
//    }
Run Code Online (Sandbox Code Playgroud)

Raw*_*ugs 9

我刚刚遇到了类似的情况,我能够belongsToThrough完成hasOneThrough

public function country()
{
    return $this->hasOneThrough(
        Country::class,       // model we are trying to get
        District::class,      // model we have an _id to
        'id',                 // WHERE `district`.`id` = `city`.`district_id`
        'id',                 // `countries`.`id`
        'district_id',        // local column relation to our through class
        'country_id'          // `district`.`country_id`
    );
}
Run Code Online (Sandbox Code Playgroud)

这应该生成的是

SELECT * FROM `countries` 
    INNER JOIN `districts` 
        ON `districts`.`country_id` = `countries`.`id`
    WHERE `districts`.`id` = ?


-- ? == city.district_id
Run Code Online (Sandbox Code Playgroud)

数据库结构:

City:
    id: increments
    district_id: integer
    ...

Country:
    id: increments
    ...

District:
    id: increments
    country_id: integer
    ...
Run Code Online (Sandbox Code Playgroud)

然后我们可以做$city->country

注意:我还没有完全测试过这个,但经过我所做的测试,它“有效”

编辑:我最初认为我需要将 localKey 参数保留为空,否则关系将无法工作。事实证明我并没有完全理解该专栏在做什么,这是错误的。该键是与我们的 through 列相关的本地列(除非我还有更多需要学习/弄清楚),当将该值保留为 null 时,它将使用本地 id 列,其中 a. 是错误的值,b.也可能超出范围(这就是我发现它使用错误值的方式)

在我的测试中,我只有两行,两者具有相同的关系。但我没有意识到的是,在“通过表”上,第 1 行和第 2 行都有相同的相关(试图达到的关系),所以我没有立即注意到这个问题。希望现在一切正常


Nis*_*K.R 3

为什么不能使用父方法?

$city = City::find(1);
$country = $city->district->country();
Run Code Online (Sandbox Code Playgroud)