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

查看:115
本文介绍了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




image_tag




- id
- image_id
- tag_id


>
在我的模型中 Image ,我有 belongsToMany('Tag')我的模型为标签,我有 belongsToMany('Image')。这是所有的工作,我希望,到目前为止。



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摩天大楼,我得到一个包含标签 nyc 或标签摩天大楼的图片列表。但是,我只想显示标签 nyc 和标签摩天大楼的图片。



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'其中'tags''''''''''''''''''''?' code>和摩天大楼

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 标签的图像,然后返回图像的标签摩天大楼),然后选择两个结果中出现的图像,但我不确定实现这一目标的最佳方法是,以及它是否是最有效的方式。

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.

我怀疑使用查询构建器可以轻松地执行此操作, m目前缺少!

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.

推荐答案

具有的手动计数和可以正常工作,但可以使用简单的 whereHas / p>

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的解决方案只能在枢轴上没有重复的情况下工作表。使用Eloquent的附加方法在 BelongsToMany 关系可能会导致这种情况。

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天全站免登陆