我把自己混为一谈。 希望你们都能提供帮助。
我在评估中有几个不同的表格:
inspections
(a basic table that just holds id, date, etc.; all we need from this is the id)customers
(table contains the id and other customer info)customer_types
(table holds the different customer types, e.g. Buyer, Buyer s Agent, etc.)inspection_customers_types
(this table relates the inspection [inspection_id
], the customer [customer_id
], and the customer type [type_id
])
基本上,客户与<条码>查询<>和<条码>和<条码>_ 类型条码>表有着许多到许多关系。
例如:
客户<代码>{ID:345}可归属:{ID: 10}
, 客户类型<代码>{ID: 3 - 买方 s Agent}
Customer {ID: 345}
can belong to Inspection {ID: 15}
with Customer Type {ID: 1 - Buyer}
我认为,Im试图获取customer_types.type
。 尽管我看不到工作的价值,但我认为我对关系的定义是不正确的,但我看不到这种关系是正确的。 我列举我的关系如下:
<>光>
public function customers()
{
return $this->belongsToMany(Customer::class, inspection_customers_types )
->withPivot( type_id )
->withTimestamps();
}
<><>Customer Model
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>
这里,我已经 st了......目前,我发现这一错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column inspection_customers_types.customer_type_id in field list
But I don t see anywhere that I m calling customer_type_id
so I have no idea why it would be giving me this error. This is the SQL statement it spits out if that helps anyone:
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