Laravel - 自定义中间表模型 - 急切加载

问题描述 投票:0回答:1

这是我当前的设置:

<?php

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Employee extends Model
{
    protected $table = 'employees';

    protected $fillable = [
        'first_name', 'last_name', 'gender',
        'birthdate', 'nationality_id', 'company_id',
    ];

    public function positions(): BelongsToMany
    {
        return $this->belongsToMany(Position::class)
            ->using(EmployeePosition::class);
    }
}

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Position extends Model
{
    protected $table = 'positions';

    protected $fillable = [
        'name',
    ];

    public function employees(): BelongsToMany
    {
        return $this->belongsToMany(Employee::class)
            ->using(EmployeePosition::class);
    }
}

它们通过数据透视表相互连接,根据文档将其配置为“自定义中间表模型”。

我设置了以下列:

| id | employee_id | position_id |

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\Pivot;

class EmployeePosition extends Pivot
{
    /**
     * Indicates if the IDs are auto-incrementing.
     *
     * @var bool
     */
    public    $incrementing = true;
    protected $table        = 'employee_position';
}

有了

$employee
,到目前为止一切正常:
$employee->load('positions')

接下来,我还创建了一个

employee_position_technologies
表:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class EmployeePositionTechnology extends Model
{
    protected $table = 'employee_position_technologies';

    protected $fillable = [
        'employee_position_id',
        'technology_id',
    ];
}

我想给“员工职位”分配很多“技术”。 我也做到了。

现在,我想要实现的是将技术相应地加载到员工职位上:

$employee->load('positions.technologies')

为此,我修改了

EmployeePosition
类,添加了
technologies()
方法,如下所示:

    public function technologies()
    {
        return $this->hasManyThrough(
            Technology::class,
            EmployeePositionTechnology::class,
            'employee_position_id',
            'id',
            'position_id',
            'technology_id'
        );
    }

这不起作用,因为

technologies()
方法似乎是从
Position
模型。

我发现要访问“中间表模型”,我们必须使用以下命令:

$employee->position->pivot->technologies

所以我尝试了:

$employee->load('positions.pivot.technologies');

这似乎也不起作用,有错误

Call to undefined relationship [pivot] on model [App\Models\Position].

接下来,虽然我不喜欢,但我尝试了:

$employee->load('positions');

$employee->positions->each(function ($position) {
    $position->pivot->load('technologies');
});

这似乎有效,我可以访问该方法,但

technologies
的结果
position
关系为空。

我尝试转储查询,它似乎是正确的:

select * from "technologies" 
inner join "employee_position_technologies" 
on "employee_position_technologies"."technology_id" = "technologies"."id" 
where "employee_position_technologies"."employee_position_id" = ?

我错过了什么?

laravel eloquent eager-loading eloquent-relationship
1个回答
0
投票

您是否尝试过使用 with() 方法代替 load() 方法来急切加载您要加载的关系?您可以使用 with 关键字来实现此目的,如下所示,其中关系作为参数传递给 with 方法

$employee->positions()->with('technologies')->get();

这将返回为该员工找到的所有职位以及每个职位的相关技术(如果有)。您可以从 Laravel 官方网站

阅读更多有关使用 with 方法进行预加载的信息
© www.soinside.com 2019 - 2024. All rights reserved.