简体   繁体   English

Laravel雄辩的hasmany的hasmany

[英]Laravel eloquent has hasmany of hasmany

How to show all company who has photo attachment? 如何显示所有有照片附件的公司?

company: 公司:

id | name 

user: 用户:

id | name | company_id

post: 职位:

id | user_id | text

post_attachment: post_attachment:

id | post_id | path | type

company model: 公司型号:

public function users() {
  return $this->hasMany('App\User')->orderBy('id', 'ASC');
}

user model: 用户模型:

public function posts() {
  return $this->hasMany('App\Post');
}

posts model: 帖子模型:

public function images(){
  return $this->hasMany('App\PostsAttachment')->where("type", "image");
}

I want to get all company who have user(s) that have post(s) with minimal 2 images attachment. 我想获得所有拥有用户的公司,这些用户的帖子至少包含2张图片。 Anyone can help me? 有人可以帮助我吗?

I tried Company::has('users.posts.images', '<', 2)->get(); 我尝试了Company::has('users.posts.images', '<', 2)->get(); but it also give companies who has user image below 2. 但同时也会为用户形象低于2的公司提供服务。

Using eloquent you could write your logic as 用口才,你可以写你的逻辑为

$companies = Company::has('users.posts.images')->get();

See Querying Relationship Existence 请参阅查询关系存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM