Laravel的下落返回意外结果
我有2个桌子艺术家和艺术品。其结构是:
ID
名称 | 年龄 | 1 |
---|---|---|
Joe | Blogg | 2 22 |
2 | Bob Duff | 34 |
3 | Mark Smith | 25 |
Artworks
ID | 标题 | 出售 | 价格 | Artist Artist Artist_id |
---|---|---|---|---|
1 | Flowers | 1 | 80.00 | 1 |
2 | 植物 | 1 | 50.00 | 2 |
3 | Boxes | 0 | 100.00 | 2 |
3 | 螺纹 | 0 | 110.00 | 3 |
艺术家 艺术品有关系
public function artworks()
{
return $this->hasMany(Artwork::class);
}
艺术家模特我与艺术品模型中的
public function artist()
{
return $this->belongsTo(Artist::class);
}
,我与我想退还所有出售艺术品的艺术家的艺术家有关系(出售为1),是卖艺术品的Ans Ans价格的名称。我正在使用的雄辩的查询是:
$artists = Artist::with('artworks')->whereRelation('artworks','sold','=',1)->get();
这确实选择了所有售出艺术品的艺术家。但是,如果艺术家既有售出的艺术品,又有未售出的艺术品,则可以返回两者。因此查询将返回:
ID | 名称 | 标题 | 出售 | 价格 | Artistes_id |
---|---|---|---|---|---|
1 | Joe Blogg | 花 | 1 | 80.00 | 1 |
2 | Bob Duff | 植物 | 1 | 50.00 | 2 |
3 | Bob Duff | Boxes | 0 | 100.00 | 2 |
当尚未出售带有标题框的艺术品时,不应在结果
如何获得它,以便我只返回出售艺术品?
I have 2 tables Artists and Artworks. The structure of which are:
Artists
id | name | age |
---|---|---|
1 | Joe Blogg | 22 |
2 | Bob Duff | 34 |
3 | Mark Smith | 25 |
Artworks
id | title | sold | price | artist_id |
---|---|---|---|---|
1 | flowers | 1 | 80.00 | 1 |
2 | plants | 1 | 50.00 | 2 |
3 | boxes | 0 | 100.00 | 2 |
3 | threads | 0 | 110.00 | 3 |
In the Artist Model I have a relation to artworks
public function artworks()
{
return $this->hasMany(Artwork::class);
}
In the Artwork Model I have the relation to artists
public function artist()
{
return $this->belongsTo(Artist::class);
}
I want to return all the artists with that have sold an artwork (sold is 1) and the name of the ans price of the sold artwork. The eloquent query I am using is:
$artists = Artist::with('artworks')->whereRelation('artworks','sold','=',1)->get();
This does select all the Artists with sold artwork. However, if the artist has both a sold artwork and an unsold artwork it returns both. so the query would return:
id | Names | title | sold | price | artist_id |
---|---|---|---|---|---|
1 | Joe Blogg | flowers | 1 | 80.00 | 1 |
2 | Bob Duff | plants | 1 | 50.00 | 2 |
3 | Bob Duff | boxes | 0 | 100.00 | 2 |
When the artwork with the title Boxes has not been sold, so should not be in the results
How do I get it so that I only return sold artworks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢科迪
几乎取得了正确的答案 - 但是现在,无论他们是否出售艺术品,我现在都会吸引所有艺术家。尽管该子查询现在仅选择出售艺术品。
我已经将下落添加到您的答案中,现在它可以工作
Thanks Cody Brew
Almost the right answer - but I am now getting all Artists whether they've sold an Artwork or not. Although the subquery now only selects sold Artwork.
I have added back the whereRelation to your answer and it now apperas to work