通过链接插入关系 table
Inserting relationship with linking table
我在使用关系模型插入新记录时遇到了一些问题。我有 3 个表 Contacts
、Contact_Customer
和 Customers
。型号如下:
Contact.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');
}
}
Customer.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>
所以视图看不到新的关系。我尝试在 slack 上使用多种解决方案并阅读 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');
}
}
我在使用关系模型插入新记录时遇到了一些问题。我有 3 个表 Contacts
、Contact_Customer
和 Customers
。型号如下:
Contact.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');
}
}
Customer.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>
所以视图看不到新的关系。我尝试在 slack 上使用多种解决方案并阅读 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');
}
}