Laravel 一对多关系
Laravel One-to-many relationship
尝试在 Laravel 5.4 中建立一对多关系。我已经在线阅读了文档和其他资源,但在每种情况下都无法正常工作,而且我尝试过的各种方法都会导致不同的错误。
我有以下三个table:
- 帐户
- 联系人
- account_contact (pivot table) 有字段:account_id (int/Foreign key), contact_id (int/Foreign key), primary (int), billing (int)
我正在努力使帐户可以(但不一定)有一个或多个联系人。
我的模型如下:
帐户
public function contacts()
{
return $this->hasMany(Contact::class);
}
接触
public function account()
{
return $this->belongsTo(Account::class)->withPivot('primary', 'billing');
}
然后在我尝试的控制器中说:
$account = Account::find($id);
if($account->isEmpty())
return response()->json(['Account not found.'], 404);
$contact = $account->contacts()->exists();
我收到以下错误:
(1/1) BadMethodCallException
方法联系人不存在。
显然,我正在尝试做的是,当建立联系时,它可以通过 Pivot table 附加到帐户。
在获取帐户时,我可以通过 Pivot table 获取额外的 Pivot table 字段,以及属于该帐户的联系人。
只是为了进一步澄清,我正在尝试使用 eloquent,使用数据透视来执行以下查询,而不必在每个实例中都写出来。
$contacts = DB::table('account_contact')
->select('contact_id', 'primary', 'billing')
->where('account_id', $id)
->get();
$accountContacts = [];
foreach($contacts as $c){
$accountContact = Contact::find($c->id);
$accountContacts[] = array(
"id" => $accountContact->id,
"sal" => $sal = $accountContact->salutation == null? '' : $accountContact->salutation,
"firstName" => $accountContact->first_name,
"lastName" => $accountContact->last_name,
);
}
我希望能够做类似的事情
$accounts->pivot->联系人
并获得这样的名称:
$accounts->pivot->contacts->first_name
在您的 account
模型中,建立如下关系:
public function account_contact()
{
return $this->belongsToMany('App\Account', 'account_contact', 'account_id', 'contact_id');
}
然后使用您编写的函数获取。我希望它能奏效。
请试一试,然后告诉我效果如何。
您的关系是多对多关系,因此您需要这样做:
帐户
public function contacts()
{
return $this->belongsToMany(Contact::class)->withPivot('primary', 'billing');
}
接触
public function account()
{
return $this->belongsToMany(Account::class)->withPivot('primary', 'billing');
}
那么你应该通过 eloquent 一直这样做以避免 N+1 issue
$account = Account::with("contacts")->where("id", $id)->get();
foreach ($account->contacts as $contact) {
$accountContacts[] = array(
"id" => $accountContact->id,
"sal" => $sal = $accountContact->salutation == null? '' :
$accountContact->salutation,
"firstName" => $accountContact->first_name,
"lastName" => $accountContact->last_name,
);
}
尝试在 Laravel 5.4 中建立一对多关系。我已经在线阅读了文档和其他资源,但在每种情况下都无法正常工作,而且我尝试过的各种方法都会导致不同的错误。
我有以下三个table:
- 帐户
- 联系人
- account_contact (pivot table) 有字段:account_id (int/Foreign key), contact_id (int/Foreign key), primary (int), billing (int)
我正在努力使帐户可以(但不一定)有一个或多个联系人。
我的模型如下:
帐户
public function contacts()
{
return $this->hasMany(Contact::class);
}
接触
public function account()
{
return $this->belongsTo(Account::class)->withPivot('primary', 'billing');
}
然后在我尝试的控制器中说:
$account = Account::find($id);
if($account->isEmpty())
return response()->json(['Account not found.'], 404);
$contact = $account->contacts()->exists();
我收到以下错误:
(1/1) BadMethodCallException 方法联系人不存在。
显然,我正在尝试做的是,当建立联系时,它可以通过 Pivot table 附加到帐户。 在获取帐户时,我可以通过 Pivot table 获取额外的 Pivot table 字段,以及属于该帐户的联系人。
只是为了进一步澄清,我正在尝试使用 eloquent,使用数据透视来执行以下查询,而不必在每个实例中都写出来。
$contacts = DB::table('account_contact')
->select('contact_id', 'primary', 'billing')
->where('account_id', $id)
->get();
$accountContacts = [];
foreach($contacts as $c){
$accountContact = Contact::find($c->id);
$accountContacts[] = array(
"id" => $accountContact->id,
"sal" => $sal = $accountContact->salutation == null? '' : $accountContact->salutation,
"firstName" => $accountContact->first_name,
"lastName" => $accountContact->last_name,
);
}
我希望能够做类似的事情 $accounts->pivot->联系人 并获得这样的名称: $accounts->pivot->contacts->first_name
在您的 account
模型中,建立如下关系:
public function account_contact()
{
return $this->belongsToMany('App\Account', 'account_contact', 'account_id', 'contact_id');
}
然后使用您编写的函数获取。我希望它能奏效。 请试一试,然后告诉我效果如何。
您的关系是多对多关系,因此您需要这样做:
帐户
public function contacts()
{
return $this->belongsToMany(Contact::class)->withPivot('primary', 'billing');
}
接触
public function account()
{
return $this->belongsToMany(Account::class)->withPivot('primary', 'billing');
}
那么你应该通过 eloquent 一直这样做以避免 N+1 issue
$account = Account::with("contacts")->where("id", $id)->get();
foreach ($account->contacts as $contact) {
$accountContacts[] = array(
"id" => $accountContact->id,
"sal" => $sal = $accountContact->salutation == null? '' :
$accountContact->salutation,
"firstName" => $accountContact->first_name,
"lastName" => $accountContact->last_name,
);
}