English 中文(简体)
Laravel Eloquent Relations with 许多国家
原标题:Laravel Eloquent Relationships with Many to Many

我把自己混为一谈。 希望你们都能提供帮助。

我在评估中有几个不同的表格:

  1. inspections (a basic table that just holds id, date, etc.; all we need from this is the id)
  2. customers (table contains the id and other customer info)
  3. customer_types (table holds the different customer types, e.g. Buyer, Buyer s Agent, etc.)
  4. 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
问题回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签