外键查询laravel-雄辩的方法 [英] Foreign key query laravel - the eloquent way
本文介绍了外键查询laravel-雄辩的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正试图找到一种方法来雄辩地查询我的模型.
I am trying to find a way to write eloquent way to query my model.
我有一个Form
模型,每个表单都属于一个User
(Form:User = 1:1).每个User
都有一个State
和一个City
与其关联. Admin
查看一个Form
,每个管理员可以分配给多个State
和City
.
I have a Form
model every form belongs to a User
(Form:User = 1:1). Each User
has a State
and a City
associated with them. Admin
reviews a Form
and each admin can be assigned to multiple State
and City
.
我想找到属于Admin
的Form
.
这是Admin.php(模型)中的表单功能
This is the forms function in Admin.php (Model)
public function forms()
{
//cities
$cities = $this->cities->pluck('name');
//states
$states = $this->states->pluck('name');
//get all form from the user and states
$forms = Form::whereHas('user',function ($query) use($cities,$states)
{
// find form from his states or cities
$query->whereIn('state',$states)->orWhereIn('city',$cities);
});
return $forms;
}
当前,它返回所有表格. 任何帮助将不胜感激!!!
Currently it returns all the forms. Any help will be appreciated!!!
推荐答案
您可以尝试以下方法:
$citiesForms =$this->cities->forms->toArray();
$statesForms = $this->states->forms->toArray();
return array_merge($citiesForms, $statesForms);
,您可以查找hasManyThrough
以使此操作一行完成
and you can look for hasManyThrough
to make this done in one line
这篇关于外键查询laravel-雄辩的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文