简体   繁体   English

有没有办法在 laravel 中使用 GET 方法中的变量来使用 where like 查询?

[英]is there a way to use where like query in laravel with variable from GET method?

i want to filter with like in laravel project and paginate with laravel paginate, i try this code我想在 laravel 项目中过滤并用 laravel 分页,我试试这个代码

$search = $request->get('query');  
$products= DB::table('products')
           ->where('nama_produk', 'LIKE', '%'.$search.'%') 
           ->paginate(6);

when use this code nothing error but it return all of data in my database table and not filtered.使用此代码时没有错误,但它返回我的数据库表中的所有数据并且未过滤。

if i use this code如果我使用此代码

$search = $request->get('query');  
$products= DB::table('products')
           ->where('nama_produk', 'LIKE', '%test%') 
           ->paginate(6);

it Worked:( can someone help me to solve this problem?它有效:(有人可以帮我解决这个问题吗?

This is my search controller:这是我的搜索 controller:

public function search(Request $request){
  $query = $request->query;
  $search = Post::where('post_title', 'LIKE', "%$query%")->orWhere('post_body', 'LIKE', "%$query%")->orWhere('post_tags', 'LIKE', "%$query%")->orderBy('created_at')->paginate(10);
  return view('front.search', ['posts'=> $search, 'query'=> $query]);
}

Hope this helps.希望这可以帮助。

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

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