【发布时间】:2017-04-28 00:50:31
【问题描述】:
我有一个 admin 表,admin 有很多 form。每个form 都分配给他们仪表板上显示的admin。
问题是form 可能没有分配给任何admin。
我想得到所有这些forms 任何帮助表示赞赏。谢谢!
编辑:Admin 与 form 通过自定义关系相关,如 Here 所述
总结一下,
Admin.php
public function states(){
return $this->belongsToMany('App\State');
}
public function cities()
{
return $this->belongsToMany('App\City');
}
//gets the forms in this admin's city or state
//Let me know if there is a better way to do this, i feel like im overdoing stuff here
public function forms()
{
//cities
$cities = $this->cities->pluck('name');
//states
$states = $this->states->pluck('name');
$users = User::whereIn('state',$states)>orWhereIn('city',$cities)->get()->pluck('id');
$forms = Form::whereIn('user_id',$users);
return $forms;
}
我想获取不属于任何admin的表单
【问题讨论】:
-
您基本上必须在表单模型上定义该关系的逆向。
-
我已经添加了代码,你能指出我正确的方向吗?
标签: php laravel laravel-query-builder