Laravel 如何通过数据透视表获取相关字段列表

时间:2021-03-08 10:57:14

标签: laravel eloquent pivot-table

我有 4 个表:peoples、companys、countries 和包含 people_id 和 company_id 的数据透视表 company_people(因为 peoples 和公司都属于 many)。

在 People 模型中,我有以下功能:

class People extends Model
{
    // main company (only one)
    public function company()
    {
        return $this->belongsTo(Company::class);
    }
    // all other companies
    public function companies()
    {
        return $this->belongsToMany(Company::class);
    }
    public function country()
    {
        return $this->belongsTo(Country::class);
    }
}

然后在 People 控制器中,我有以下内容,以便准备将所有与相关主要公司名称(只有一个)、国家名称(只有一个)和其他公司的人员列表显示为名称列表.我可以做前两个,但不能做最后一个。我该怎么做?

$peoples = People::orderBy($sortField,$sortOrder)
    ->with(['companies','company','country'])
    ->get();

foreach ($peoples as $people) {
    $people->company = '['.$people->company->company.']'; // main company name
    $people->country = '['.$people->country->country.']'; // country name
    $people->otherCompanies = ? // list of other company names through pivot table
}

这里是 4 个表的所有结构:

CREATE TABLE `company_people` (
  `id` bigint NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `company_id` bigint UNSIGNED NOT NULL,
  `people_id` bigint UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


CREATE TABLE `countries` (
  `id` varchar(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'AA',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `is_active` int NOT NULL DEFAULT '1',
  `country` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


CREATE TABLE `peoples` (
  `id` bigint UNSIGNED NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `is_active` int NOT NULL DEFAULT '1',
  `firstname` varchar(120) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `lastname` varchar(120) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '',
  `country_id` varchar(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'ZA',
  `company_id` bigint UNSIGNED DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci PACK_KEYS=0;

ALTER TABLE `company_people`
  ADD PRIMARY KEY (`id`),
  ADD KEY `article_id` (`company_id`),
  ADD KEY `tag_id` (`people_id`);

ALTER TABLE `countries`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `peoples`
  ADD PRIMARY KEY (`id`),
  ADD KEY `country_id` (`country_id`),
  ADD KEY `company_id` (`company_id`);

ALTER TABLE `company_people`
  MODIFY `id` bigint NOT NULL AUTO_INCREMENT;

ALTER TABLE `peoples`
  MODIFY `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `peoples`
  ADD CONSTRAINT `peoples-company` FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `peoples-country` FOREIGN KEY (`country_id`) REFERENCES `countries` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;

ALTER TABLE `companies`
  ADD CONSTRAINT `peoples-country` FOREIGN KEY (`country_id`) REFERENCES `countries` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;

ALTER TABLE `company_people`
  ADD CONSTRAINT `companies-peoples` FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `peoples-companies` FOREIGN KEY (`people_id`) REFERENCES `peoples` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,

1 个答案:

答案 0 :(得分:2)

您可以使用 pluck() 获取所有公司名称,然后使用 toArray() 像这样转换为数组

$peoples = People::orderBy($sortField,$sortOrder)
    ->with(['companies','company','country'])
    ->get();

foreach ($peoples as $people) {
    $people->company = '['.$people->company->company.']'; // main company name
    $people->country = '['.$people->country->country.']'; // country name
    $people->otherCompanies = $people->companies->pluck('name')->toArray(); // list of other company names through pivot table
}
<块引用>

如果你想要 otherCompanies 名称作为逗号分隔然后使用 $people->companies->pluck('name')->join(',');