如何将枢轴关系附加或附加到模型? [英] How to append or attach a pivot relationship to a model?
本文介绍了如何将枢轴关系附加或附加到模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每当我从律师档案表格中获取律师记录时,我都会尝试从side_areas表中获取律师的执业区域记录.这两个表(practice_areas和律师个人资料)与名为(lawyer_practice_area)的数据透视表相关
I am trying to get the practice area record of lawyers from practice_areas table along side each time I get a lawyer's record from lawyer_profiles_table. This two tables (practice_areas and lawyer_profiles) are related with a pivot table named (lawyer_practice_area)
下面的代码来自LawyerProfile模型
This code below are from the LawyerProfile Model
public function practice_areas()
{
return $this->belongsToMany(PracticeArea::class, "lawyer_practice_area", "practice_area_id", "lawyer_id");
}
我尝试通过以下操作通过属性获取它
I tried getting it through an attribute by doing the below
public function getPracticeAreasAttribute()
{
return $this->practice_areas();
}
然后以这种方式附加
protected $appends = ["practice_areas"];
但是我一直在邮递员那里得到这个结果-"withtimestamps":"false"
but I keep getting this result on postman - "withtimestamps":"false"
请参见下面的输出:-
"id": 1,
"city": "Hintztown",
"state": "Iowa",
"longitude": "-163.884102",
"latitude": "-18.127927",
"lawyer_profile": {
"id": 26,
"email": "serena.barrows@yahoo.com",
"name": "Raoul Hegmann",
"primary_location_id": 1,
"photo_id": 39,
"phone_number": "887.299.6204 x456",
"about_lawyer": "Distinctio eos omnis autem error.",
"practice_areas": {
"withTimestamps": false
}
}
},
推荐答案
可能要使用的是看看文档.
$books = App\Book::with('author')->get();
foreach ($books as $book) {
echo $book->author->name;
}
这篇关于如何将枢轴关系附加或附加到模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文