Laravel 与多对多的雄辩关系

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

我自己都有点困惑了。希望大家能够帮忙。

我的应用程序中有几个不同的表格:

  1. inspections
    (仅保存 id、日期等的基本表;我们需要的只是 id)
  2. customers
    (表包含id和其他客户信息)
  3. customer_types
    (表格包含不同的客户类型,例如买家、买家代理等)
  4. inspection_customers_types
    (此表涉及检查[
    inspection_id
    ]、客户[
    customer_id
    ]和客户类型[
    type_id
    ])

基本上,客户与

inspection
customer_types
表具有多对多关系。

例如:

客户

{ID: 345}
可以属于检验
{ID: 10}
,客户类型为
{ID: 3 - Buyer's Agent}

客户

{ID: 345}
可以属于检验
{ID: 15}
,客户类型为
{ID: 1 - Buyer}

在我看来,我正在尝试获取

customer_types.type
值,但我似乎无法让它发挥作用,我认为我错误地定义了关系,但我似乎无法让它们正确。我将在下面列出我的关系:

检验模型

    public function customers() 
    {
        return $this->belongsToMany(Customer::class, 'inspection_customers_types')
            ->withPivot('type_id')
            ->withTimestamps();
    }

客户模型

    public function customerTypes()
    {
        return $this->belongsToMany(CustomerType::class, 'inspection_customers_types')
                    ->withPivot('inspection_id')
                    ->withTimestamps();
    }

    public function inspections()
    {
        return $this->belongsToMany(Inspection::class, 'inspection_customers_types')
                    ->withPivot('type_id')
                    ->withTimestamps();
    }

现在,在我的控制器中,我渴望加载检查:

$inspection->load('invoice', 'services', 'vendors', 'statusNotes', 'fileUploads', 'customers');

并将其传递到我的视图,我尝试向每个客户显示他们的相关类型名称:

    <div class="row row-cols-4 mb-3">
        @foreach ($inspection->customers as $customer)
        <div class="col">
                <div class="card">
                    <div class="card-header">
                        <h5>{{ $customer->customerTypes->name }}</h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col">
                                <p class="m-0">{{ $customer->first_name . ' ' . $customer->last_name }}</p>
                                @if ($customer->phone_number_1)
                                    <p class="m-0">{{ $customer->phone_number_1 }}</p>
                                @endif
                                @if ($customer->phone_number_2)
                                    <p class="m-0">{{ $customer->phone_number_2 }}</p>
                                @endif
                                @if ($customer->email_1)
                                    <p class="m-0">{{ $customer->email_1 }}</p>
                                @endif
                                @if ($customer->email_2)
                                    <p class="m-0">{{ $customer->email_2 }}</p>
                                @endif
                                <span class="fw-bold">Lifetime Value:</span> ${{ $customer->lifetime_value }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        @endforeach
    </div>

这就是我陷入困境的地方......目前,我收到此错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'inspection_customers_types.customer_type_id' in 'field list'

但是我没有看到任何地方我正在调用

customer_type_id
,所以我不知道为什么它会给我这个错误。这是它吐出的 SQL 语句,如果对任何人有帮助的话:

SELECT
  `customer_types`.*,
  `inspection_customers_types`.`customer_id` AS `pivot_customer_id`,
  `inspection_customers_types`.`customer_type_id` AS `pivot_customer_type_id`,
  `inspection_customers_types`.`inspection_id` AS `pivot_inspection_id`,
  `inspection_customers_types`.`created_at` AS `pivot_created_at`,
  `inspection_customers_types`.`updated_at` AS `pivot_updated_at`
FROM
  `customer_types`
  INNER JOIN `inspection_customers_types` ON `customer_types`.`id` = `inspection_customers_types`.`customer_type_id`
WHERE
  `inspection_customers_types`.`customer_id` = 1
php laravel eloquent many-to-many eloquent-relationship
1个回答
0
投票

Laravel 默认情况下无法自动设置关系,我们必须更具体地说明这一点:

class Inspection
{
    public function customers() 
    {
        return $this->belongsToMany(Customer::class, 'inspection_customers_types', 'inspection_id', 'customer_id')
            ->withPivot('type_id')
            ->withTimestamps();
    }
}

并且:

class Customer
{
    public function customerTypes()
    {
        return $this->belongsToMany(CustomerType::class, 'inspection_customers_types', 'customer_id', 'type_id')
                    ->withPivot('inspection_id')
                    ->withTimestamps();
    }

    public function inspections()
    {
        return $this->belongsToMany(Inspection::class, 'inspection_customers_types', 'customer_id', 'inspection_id')
                    ->withPivot('type_id')
                    ->withTimestamps();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.