Laravel 查询 'where' 和 'orwhere' 无法正常工作
Laravel query with having 'where' and 'orwhere' not working properly
我有以下功能 -
public function search_profile(Request $request)
{
$search_keyword = $request->search;
$search_user = $request->user;
$user_public = User::where('username', $request->user)->first();
$tags_list = Tag::orderBy('tag', 'asc')->get();
$bookmarks = Bookmark::orderBy('created_at','desc')->where('bookmarker', $search_user)->where('public', '1')->where('tags', 'rlike', $search_keyword)->orwhere('title', 'rlike', $search_keyword)->orwhere('description', 'rlike', $search_keyword)->orwhere('contents', 'rlike', $search_keyword)->paginate(15);
$bookmarks_all = Bookmark::orderBy('created_at','desc')->where('bookmarker', $search_user)->where('public', '1')->where('tags', 'rlike', $search_keyword)->orwhere('title', 'rlike', $search_keyword)->orwhere('description', 'rlike', $search_keyword)->orwhere('contents', 'rlike', $search_keyword)->get();
return view('profile_search')->with('bookmark', $bookmarks)->with('tags_list', $tags_list)->with('bookmarks_all', $bookmarks_all)->with('username', $user_public)->with('search_keyword', $search_keyword);
}
但我什至从其他 bookmarker
那里得到了结果。例如 - 如果 $search_keyword = 'laravel'
和 $serach_user = 'zack'
。我也从其他用户那里得到了结果。
我是 laravel 的新手。那么我在查询中做错了什么?
您的查询
$bookmarks = Bookmark::orderBy('created_at','desc')->where('bookmarker', $search_user)->where('public', '1')->where('tags', 'rlike', $search_keyword)->orwhere('title', 'rlike', $search_keyword)->orwhere('description', 'rlike', $search_keyword)->orwhere('contents', 'rlike', $search_keyword)->paginate(15);
会给你这样的东西
SELECT * FROM bookmarks WHERE bookmarker = $search_user AND public = 1 OR title rlike $search_keyword OR content rlike $search_keyword;
这不是您想要的,因为如果它与标题匹配,它将 return 结果。
您最可能想要的是:
Bookmark::orderBy('created_at','desc')
->where('bookmarker', '$search_user')
->where('public', '1')
->orWhere(function ($query) use ($search_keyword) {
$query->where('title', 'rlike', $search_keyword)
->where('contents', 'rlike', $search_keyword);
})
->get();
查看 advance where。
嘿,Xahed Kamal,请使用此查询与 table 像这里一样($data = DB::table('table_name')->where('column_name',abcd)-> orWhere('column_name',abcd)->get();)
它工作正常
还有下一个
$bookmarks = Bookmark::orderBy('created_at','desc')->哪里('bookmarker', $search_user)->哪里('public', '1 ')->where('tags', 'rlike', $search_keyword)->pluck('id');
$bookmarks_data = Bookmark::whereIn('id',$bookmarks)->orwhere('title', 'rlike', $search_keyword) ->orwhere('description', 'rlike', $search_keyword)->orwhere('contents', 'rlike', $search_keyword)->分页( 15);
我有以下功能 -
public function search_profile(Request $request)
{
$search_keyword = $request->search;
$search_user = $request->user;
$user_public = User::where('username', $request->user)->first();
$tags_list = Tag::orderBy('tag', 'asc')->get();
$bookmarks = Bookmark::orderBy('created_at','desc')->where('bookmarker', $search_user)->where('public', '1')->where('tags', 'rlike', $search_keyword)->orwhere('title', 'rlike', $search_keyword)->orwhere('description', 'rlike', $search_keyword)->orwhere('contents', 'rlike', $search_keyword)->paginate(15);
$bookmarks_all = Bookmark::orderBy('created_at','desc')->where('bookmarker', $search_user)->where('public', '1')->where('tags', 'rlike', $search_keyword)->orwhere('title', 'rlike', $search_keyword)->orwhere('description', 'rlike', $search_keyword)->orwhere('contents', 'rlike', $search_keyword)->get();
return view('profile_search')->with('bookmark', $bookmarks)->with('tags_list', $tags_list)->with('bookmarks_all', $bookmarks_all)->with('username', $user_public)->with('search_keyword', $search_keyword);
}
但我什至从其他 bookmarker
那里得到了结果。例如 - 如果 $search_keyword = 'laravel'
和 $serach_user = 'zack'
。我也从其他用户那里得到了结果。
我是 laravel 的新手。那么我在查询中做错了什么?
您的查询
$bookmarks = Bookmark::orderBy('created_at','desc')->where('bookmarker', $search_user)->where('public', '1')->where('tags', 'rlike', $search_keyword)->orwhere('title', 'rlike', $search_keyword)->orwhere('description', 'rlike', $search_keyword)->orwhere('contents', 'rlike', $search_keyword)->paginate(15);
会给你这样的东西
SELECT * FROM bookmarks WHERE bookmarker = $search_user AND public = 1 OR title rlike $search_keyword OR content rlike $search_keyword;
这不是您想要的,因为如果它与标题匹配,它将 return 结果。
您最可能想要的是:
Bookmark::orderBy('created_at','desc')
->where('bookmarker', '$search_user')
->where('public', '1')
->orWhere(function ($query) use ($search_keyword) {
$query->where('title', 'rlike', $search_keyword)
->where('contents', 'rlike', $search_keyword);
})
->get();
查看 advance where。
嘿,Xahed Kamal,请使用此查询与 table 像这里一样($data = DB::table('table_name')->where('column_name',abcd)-> orWhere('column_name',abcd)->get();)
它工作正常
还有下一个 $bookmarks = Bookmark::orderBy('created_at','desc')->哪里('bookmarker', $search_user)->哪里('public', '1 ')->where('tags', 'rlike', $search_keyword)->pluck('id');
$bookmarks_data = Bookmark::whereIn('id',$bookmarks)->orwhere('title', 'rlike', $search_keyword) ->orwhere('description', 'rlike', $search_keyword)->orwhere('contents', 'rlike', $search_keyword)->分页( 15);