简体   繁体   English

Laravel 5.4 - 查询与搜索termn的关系

[英]Laravel 5.4 - Query relationship with search termn

i'm working in laravel 5.4, i have this tables: 我在laravel 5.4工作,我有这个表:

PROJECT | 项目 | id | id | tags | 标签| title | 标题|

PRODUCT | 产品 | id | id | project_id | project_id | category_id | category_id |

CATEGORY | 类别 | id | id | name | 名字|

The tags of project is a string separated by comma: project的标签是一个用逗号分隔的字符串:

tag1,tag2,tag3... TAG1,TAG2,TAG3 ...

The first step that i need is filter projects by termn: 我需要的第一步是按termn筛选项目:

$termn = 'examples';
$projects = Project::where('tags', 'LIKE' , '%'.$termn.'%')->paginate(20);

it work well, but i would like get all product's project with a specific "category_id". 它运作良好,但我希望所有产品的项目都具有特定的“category_id”。

My relations models is working well. 我的关系模型运作良好。

Thank you for your help! 谢谢您的帮助! this is the first time that i'm trying to do something like this. 这是我第一次尝试做这样的事情。

Use with() method: with()方法一起使用:

Project::where('tags', 'like' , '%'.$termn.'%')
       ->with(['products' => function($q) use($categoryId) {
           $q->where('category_id', $categoryId);
       }])
       ->paginate(20);

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

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