0

大家早上好,我真的因为与 Laravel 的实体关系问题而失去理智。我正在使用最新版本的 Laravel 8.x。我的数据模型组成如下。

缔约方表

  1. ID
  2. 派对详细信息 ID
  3. 派对类型

人员表

  1. ID
  2. 派对类型('P')
  3. 姓名

公司表

  1. ID
  2. 派对类型('C')
  3. 姓名

从示例中可以看出,“Parties”是包含所有各方(包括人员和公司)的表。连接键是双键,即“party_details_id”和“party_type”组成的对。在“Parties”表中,“party_details_id”列可以有两个相等的值,但“party_details_id”和“party_type”只能有一个值。我在 Laravel 上建立关系时遇到了巨大的困难。看了几篇文章,我遇到了这种情况:

缔约方模型

public function companies()
    {
        return $this->hasMany(Companies::class, 'id', 'party_details_id')->where('party_type', 'C');
    }

    public function people()
    {
        return $this->hasOne(People::class, 'id', 'party_details_id')->where('party_type', 'P');
    }

人物模型

public function party()
    {
        return $this->belongsTo(Parties::class, 'party_details_id')->where('party_type', 'P');
    }

公司模式

public function party()
    {
        return $this->belongsTo(Parties::class, 'party_details_id', 'id')->where('party_type', 'C');
    }

在测试路线中,我插入了以下代码:

Route::get('/people', function () {
    $people = new People();
    return $people->with('party')->get()->toArray();
});

我得到的是:

[
  {
    "id": 1,
    "party_type": "P",
    "name": "Alex",
    "surname": "test",
    "created_at": "2021-09-20T20:14:07.000000Z",
    "updated_at": null,
    "party": null
  }
]

从答案中可以看出,“聚会”是空的。为什么?从望远镜中查看启动的查询,我看到了这一点:

select * from `parties` where `party_type` = 'P' and 0 = 1

我究竟做错了什么?你能帮我处理好这段关系吗?谢谢

4

1 回答 1

1

我相信你的意思是党可以属于人模式或公司模式,而不是两者兼而有之。如果这是真的,那么使用多态的一对多关系。

Laravel 文档: https ://laravel.com/docs/8.x/eloquent-relationships#one-to-many-polymorphic-relations

公司

注意:受保护的 $with 属性确保 Party 始终与对象一起加载。

class Company extends Model
{
    protected $with = ['party'];

    public function party()
    {
        return $this->morphMany(Party::class,'object');
    }
}

人物模型

注意:受保护的 $with 属性确保 Party 始终与对象一起加载。

class People extends Model
{
    protected $with = ['party'];

    public function party()
    {
        return $this->morphMany(Party::class,'object');
    }
}

派对模特

然后,您将在 Party 模型上有一个“对象”关系,以便 $party->object 获取人员或公司。我建议使用“对象”关系而不是“人员”和“公司”关系,因为所有模型都会有一个或另一个,但我将两者都包括在这里以防万一。

class Party extends Model
{
    public function object()
    {
        return $this->morphTo();
    }

    public function people()
    {
        return $this->belongsTo(People::class,'object_id')->where('object_type','=',People::class);
    }

    public function company()
    {
        return $this->belongsTo(Company::class,'object_id')->where('object_type','=',Company::class);
    }
}

现在您可以使用 $party->object 或 $party->people 和 $party->company 来获取它所属的 Person 或 Company。

$party->object; // Either Company or People object
$party->people; // People object, would fail if the party belongs to Company
$party->company; // Company object, would fail if the party belongs to People 

现在,当您获得 People 或 Company 对象时,它应该自动包含 Party。

Route::get('/people', function () {
    $people = new People();
    return $people->get()->toArray();
});

如上所述,$with 属性确保每次都自动加载派对。如果您不想每次都加载派对,请删除 $with 属性并在之后加载它。

Route::get('/people', function () {
    $people = new People();
    return $people->with('party')->get()->toArray();
});
于 2021-09-28T02:07:13.090 回答