php - 插入与链接表的关系
问题描述
我在使用关系模型插入新记录时遇到了一些问题。我有 3 张桌子Contacts
和. 型号如下:Contact_Customer
Customers
联系方式.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。但我找不到解决方案。
希望你能看到我的错误,感谢阅读。
解决方案
在 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');
}
}
推荐阅读
- selenium - Robot Framework 的 Promotheus 指标是否存在?
- c++ - 用于 Linux 的 Cpp 上的计时器
- clickhouse - ClickHouse - CollapsingMergeTree 表引擎 - 记录不会合并,即使它们具有相同的列值和不同的符号(+1/-1)
- django - 如何在 django 中将 CharField 增加 1?
- bash - 使用 echo 以垂直格式打印输出以用于 bash
- c# - 如何使用访问令牌请求 microsoft azure 资源?简而言之,我们如何在 azure 中使用生成的 access_token?
- typo3 - TYPO3 中 Symfony 命令中的依赖注入
- spring - 在 JSP 表示页面中看不到消息表单 modelAndView 对象
- c# - 如何为C#中的Ctrl+C等关键中断实现异常代码
- python - 将形状 x 的数组重塑为形状 (x,y) 的数组