【发布时间】:2021-11-20 20:04:13
【问题描述】:
我怎样才能让它搜索projects.title 或items.title 并且如果它只匹配其中一个,仍然返回结果?现在如果它匹配一个项目,它不会显示,除非它也匹配一个项目标题,反之亦然。我明白为什么,但我可能不能急于做我需要做的事吗?
$query = Auth::user()->projects()->where('projects.title', 'LIKE', "%$this->search%");
$query->with(['items' => function ($query) {
$query->where('items.title', 'LIKE', "%$this->search%");
$query->with('tags');
}]);
$query->get();
我想我需要类似“如果它匹配一个或多个项目标题,返回过滤的项目和相关项目。如果它只匹配项目标题,返回项目和所有相关项目”。想法?
让它与以下内容一起工作(以及 with 部分的类似查询):
$user->projects()->with('items')->where(function($query){
$query->where('title', 'like', '...')->orWhereHas('items', function($query){
$query->where('title', 'like', '...');
});
});
【问题讨论】:
标签: php laravel database eloquent