首页 > 解决方案 > 插入与链接表的关系

问题描述

我在使用关系模型插入新记录时遇到了一些问题。我有 3 张桌子Contacts和. 型号如下:Contact_CustomerCustomers

联系方式.php

class Contact extends Model
{
    protected $fillable = [
        'FIRST_NAME',
        'INSERTION',
        'LAST_NAME',
        'EMAIL',
        'PHONE_NUMBER',
    ];

   public function contact_customers(){
        return $this->HasMany(Contact_Customer::class, 'CONTACT_ID','ID');
    }
}

Contact_Customer.php

class Contact_Customer extends Model
{
    protected $fillable = [
        'CONTACT_ID',
        'CUSTOMER_ID'
    ];

    public function customer()
    {
        return $this->belongsTo(Customer::class, 'ID');
    }

    public function contact(){
        return $this->belongsTo(Contact::class, 'ID');
    }
}

客户.php

class Customer extends Model
{
    protected $fillable = [
        'COMPANY_NAME',
        'ZIPCODE',
        'STREET',
        'HOUSE_NUMBER',
        'CITY',
        'COUNTRY',
    ];

    public function contact_customers(){
        return $this->HasMany(Contact_Customer::class, 'CUSTOMER_ID','ID');
    }

}

我在 中编写了一个函数,ContactController使用此函数向客户添加联系人:

public function create(Request $request, $customer_id)
    {
        $contact = Contact::create([
            'FIRST_NAME' => $request->input('first_name'),
            'INSERTION' => $request->input('insertion'),
            'LAST_NAME' => $request->input('last_name'),
            'EMAIL' => $request->input('email'),
            'PHONE_NUMBER' => $request->input('phone'),
        ]);

        $contact_customer = Contact_Customer::create([
            'CUSTOMER_ID' => $customer_id,
            'CONTACT_ID' => $contact->id
        ]);

        $customer = Customer::find($customer_id);
        $customer->push();

        return redirect('/customer/'.$customer_id.'/show');
    }

记录将被推送到数据库,但视图给了我这个错误:尝试获取非对象的属性“FIRST_NAME”(视图:/home/vagrant/Code/Laravel/resources/views/customer/show.blade.php )

而我的看法是这样的

<h2>Contactpersonen</h2>
        <table class="table table-borderless">
            <tbody>
            @foreach($customer->contact_customers as $contact)
                <tr>
                    <td>{{ $contact->contact->FIRST_NAME }} {{$contact->contact->INSERTION}} {{$contact->contact->LAST_NAME }}</td>
                    <td>{{ $contact->contact->PHONE_NUMBER }}</td>
                    <td>{{ $contact->contact->EMAIL }}</td>
                </tr>
            @endforeach
            </tbody>
        </table>
        <h2>Aanmaken contactpersoon</h2>
        <form method="POST" action="/customer/{{$customer->ID}}/contact/create" id="contactform">
            @csrf
            <div class="form-group">
                <label for="customer">Voornaam</label>
                <input type="text" class="form-control" id="first_name" name="first_name">
            </div>
            <div class="form-group">
                <label for="zipcode">Tussenvoegsel</label>
                <input type="text" class="form-control" id="insertion" name="insertion">
            </div>
            <div class="form-group">
                <label for="street">Achternaam</label>
                <input type="text" class="form-control" id="last_name" name="last_name">
            </div>
            <div class="form-group">
                <label for="house_number">E-mail</label>
                <input type="text" class="form-control" id="email" name="email">
            </div>
            <div class="form-group">
                <label for="country">Mobiel</label>
                <input type="text" class="form-control" id="phone" name="phone">
            </div>
            <button type="submit" class="btn btn-primary">Toevoegen</button>
        </form>

所以视图看不到新的关系。我尝试了多种解决方案,并阅读了 laracast https://laravel.com/docs/5.8/eloquent-relationships#inserting-and-updating-related-models。但我找不到解决方案。

希望你能看到我的错误,感谢阅读。

标签: phpdatabaselaraveleloquentrelationship

解决方案


在 App\Contact_Customer.php 中尝试下面给出的代码可能是您的关系未正确链接

class Contact_Customer extends Model
{
protected $fillable = [
    'CONTACT_ID',
    'CUSTOMER_ID'
];

 public function customer()
{
    return $this->belongsTo(Customer::class, 'CUSTOMER_ID', 'ID');
}

public function contact(){
    return $this->belongsTo(Contact::class, 'CONTACT_ID', 'ID');
}
}

推荐阅读