Laravel |如何执行具有多个属性的搜索 [英] Laravel | How to perform search with multiple attributes

查看:23
本文介绍了Laravel |如何执行具有多个属性的搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建财产网站,并且正在使用许多属性进行搜索,但是问题是在搜索控制器中我有非常大的代码并且很难处理,laravel中是否还有其他解决方案?

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);

推荐答案

搜索多列的最简单方法:

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);
}

这篇关于Laravel |如何执行具有多个属性的搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆