你做错了一件事,你的查询会返回完全匹配,因为你给出了确切的字符串.但是你的查询应该是这样的.
$products = Product::where('name_en','%'.$search.'%')->get();
以上查询将为您的产品提供包含搜索字符串的产品.
如果你想在关系表中搜索,那么你可以使用laravel方法join().但还有一种方法,但我总是避免使用这种方法,因为它创建了非常复杂的查询.这很重.所以你可以使用join()方法,它将使用关系表添加内连接.
以下是加入的示例:
$products = Product::join('tags',function($builder) {
$builder->on('tags.id','=','products.tag_id');
// here you can add more conditions on tags table.
})
join('sub_categories',function($builder) {
$builder->on('sub_categories.id','products.tag_id');
// here you can add more conditions on subcategories table.
})
->where('name_en','%'.$search.'%')
->get();
这是基本示例,您可以根据您的要求使用它.