Laravel | How to perform search with multiple attributes

你离开我真会死。 提交于 2021-02-17 07:22:05

问题


I am creating property website and i am doing search with number of attributes, but the problem is that in search controller i have very large code and its very difficult to handle, is there any other solution exist in laravel?

$list_property = Listing_property::where([
        ['property_type', $request['property_type']],
        ['city', $request['city']],
        ['location', $request['location']],
        ['property_area_type', $request['property_area_type']],
        ['property_size', $request['property_size']],
        ['price', $min],
        ['price', $max]
    ])
    ->orderBy('updated_at', 'DESC')
    ->paginate(21);


回答1:


The easiest way to search multiple column :

public function search(Request $request){

    $query = $request->search;
    
    $users = DB::table('users');        

   if($query){
      $users = $users->where('name', 'LIKE', "%$query%");
    }

    if($request->city){
      $users = $users->where('city',$request->city);
    }

    if($request->town){
      $users = $users->where('town', $request->town);
    }

   if($request->unit){
      $users = $users->where('unit', $request->unit);
    }

    $users->get();
    return view('users.index')->with('users', $users);
}


来源:https://stackoverflow.com/questions/63242966/laravel-how-to-perform-search-with-multiple-attributes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!