Laravel 标签搜索返回包含所有标签的图片 [英] Laravel tag search to return images that contain all tags

查看:28
本文介绍了Laravel 标签搜索返回包含所有标签的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个列出图像的应用程序,并为每个图像分配了多个标签.我希望能够找到标有所有正在搜索的标签的图像.

I'm developing an application that lists images, and has multiple tags assigned to each image. I'd like to be able to find the images that are tagged with all of the tags being searched for.


- id
- download_url
- image_width
- image_height


- id
- name


- id
- image_id
- tag_id



在我的 Image 模型中,我有 belongsToMany('Tag'),在我的 Tags 模型中,我有 <代码>belongsToMany('图片').到目前为止,这一切都如我所愿.



In my model for Image, I've got belongsToMany('Tag'), and in my model for Tags, I've got belongsToMany('Image'). This is all working as I hoped, so far.

但是,当我尝试通过它们的标签搜索图像时,我的问题就出现了.我似乎可以使用以下代码进行搜索.

However, my problem occurs when I try to search for images via their tags. I seem to be able to do an or search with the following code.

     $tags = explode(' ', $tag_text);

     $query = DB::table('images');
     $query->join('image_tag', 'images.id', '=', 'image_tag.image_id');
     $query->join('tags', 'tags.id', '=', 'image_tag.tag_id');
     foreach ($tags as $tag)
     {
          $query->orWhere('tags.name', '=', $tag);
     }

     $result = $query->get();



如果我搜索(例如)nyc skyscraper,我会得到一个带有标签 nyc 或标签 skyscraper 的图像列表.但是,我只想显示带有标签 nyc 和标签 skyscraper 的图像.



If I search for (for example) nyc skyscraper, I get a list of images that have the tag nyc or the tag skyscraper. However, I'd like to only show the images which have the tag nyc and the tag skyscraper.

我尝试将 $query->orWhere('tags.name', '=', $tag); 更改为 $query->where('tags.name', '=', $tag);,但这不会返回任何结果(可能是因为它弄乱了标签名称搜索).查询 Laravel 正在运行 "select * from 'images' inner join 'image_tag' on 'images'.'id' = 'image_tag'.'image_id' inner join 'tags' on 'tags'.'id' ='image_tag'.'tag_id' where 'tags'.'name' = ? and 'tags'.'name' = ?" 绑定 nycskyscraper.

I tried changing $query->orWhere('tags.name', '=', $tag); to $query->where('tags.name', '=', $tag);, but that then doesn't return any results (presumably because it's messing up the tag name search). The query Laravel is running "select * from 'images' inner join 'image_tag' on 'images'.'id' = 'image_tag'.'image_id' inner join 'tags' on 'tags'.'id' = 'image_tag'.'tag_id' where 'tags'.'name' = ? and 'tags'.'name' = ?" with the bindings nyc and skyscraper.

我想知道是否单独为每个标签进行图像搜索(在上面的示例中,它将返回带有标签 nyc 的图像,然后返回带有标签 skyscraper 的图像),然后挑选出两个结果中出现的图像,但我不确定实现它的最佳方法是什么,以及它是否是最有效的方法.

I wondered about doing an image search for each tag individually (which in the above example would return images with the tag nyc and then return images the tag skyscraper), and then pick out the images which occur in both results, but I'm not sure what the best way to implement this would be, and whether it's the most efficient way.

我怀疑有一种简单的方法可以使用我目前缺少的查询生成器来执行此操作!

I suspect there's an easy way of doing this using the query builder that I'm currently missing!


使它更复杂一点(并且目前不起作用)的是,除了标签搜索之外,我还想显示具有(例如)一定宽度的图像.所以在标签代码之后,我已经包含了


What complicates it a little further (and currently doesn't work), is that as well as the tag search, I'd like to show images with (for example), a certain width. So after the tag code, I've included

    $query->where('image_width', '>', 1000);

它返回所有宽度超过 1000 像素的图像,但仅当我不包含标签搜索代码时.如果我确实包含了标签搜索代码,它会忽略搜索的图像宽度部分.

which returns all images with a width over 1000 pixels, but only when I don't include the tag search code. If I do include the tag search code, it ignores the image width part of the search.

因此,理想情况下,我想返回标记为 nyc 和标记为 摩天大楼 并且 宽度超过 1000 像素.

So, ideally, I'd like to return the images which are tagged nyc and are tagged skyscraper, and are over 1000 pixels wide.

推荐答案

手动计数和 have 都可以,但您可以使用简单的 whereHas 代替:

Manual count and having would work, but you can use simple whereHas instead:

// let it be array of tag ids:
$tagsIds;

$images = Image::whereHas('tags', function ($q) use ($tagsIds) {
   $q->whereIn('tags.id', $tagsIds);
}, '=', count($tagsIds))->get();

但请注意,我的解决方案和@user3158900 的解决方案只有在数据透视表上没有重复项时才有效.在 BelongsToMany 关系上使用 Eloquent 的 attach 方法可能会导致这种情况.

However mind that both mine and @user3158900's solutions will work only, when you have no duplicates on the pivot table. Using Eloquent's attach method on BelongsToMany relation may lead to such situation.

这篇关于Laravel 标签搜索返回包含所有标签的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆