繁体   English   中英

如何反转 Eloquent has one and has many through (laravel 5.8)?

[英]How to Inverse the Eloquent Has One and Has Many Through (laravel 5.8)?

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

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

我还有三个单独的模型,其中定义了所有表之间的关系。我可以使用hasManyThrough()关系从 Country 模型中读取 City 模型的信息,但无法从 City 模型中读取 Country 信息。 我尝试使用“hasManyThrough”检索城市模型,但没有得到结果(附加为评论国家方法)。 请阅读我的模型和它的关系方法在这里..

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

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


}

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

}

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

在 Laravel 中似乎还没有一种本地方式来定义“hasManyThrough”关系的逆。 在 github 上打开了一些问题来请求它,但它们被关闭了。

如果您不介意为此功能安装第三方软件包,则可以使用staudenmeir/belongs-to-through软件包。 然后你应该能够像这样定义一个belongsToThrough关系:

class City extends Model
{
    use \Znck\Eloquent\Traits\BelongsToThrough;

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

为什么不能使用父方法?

$city = City::find(1);
$country = $city->district->country();

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

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

这应该产生的是

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


-- ? == city.district_id

数据库结构:

City:
    id: increments
    district_id: integer
    ...

Country:
    id: increments
    ...

District:
    id: increments
    country_id: integer
    ...

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

注意:我尚未对此进行全面测试,但通过测试我已经完成了它“有效”

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM