i want make complex search on table 20 columns. user has form in can choose 0 18 criteria make search. capture these criteria in controller method , return result view. problem don’t know how construct query. tried 2 criteria, below function, doesn’t return collection, it’s strange me output of dd($items) is. anyway, function doesn’t return if remove dd($items), it’s doesn’t find in table (which impossible because inserted criteria in such way function should return something).
public function looksomething($param1=null,$param2=null){ $query= "all()->"; if($param1!=null) $query.="where(’field1’,’$param1’)"."<br>->"; if($param2!=null) $query.="where(’field2’,’$param2’)"."<br>->"; $query.="get()"; $items=tablename::query(); return view('someview’,['items'=>$items]); } so, in above function, can see how thought should buid search query. resembles model had classic php. first, don’t know i’m doing wrong. second, laravel have built-in mechanism builds complex search query ?
for simple searching, define scope method in model takes array of parameters second argument, , adds clauses need. this:
class somemodel extends model { public function scopefilter($query, array $request) { if ($request->has('column')) { $query = $query->where('column', '=', $request->get('column')); } // , on... return $query; } } then can pass request parameters controller action model:
public function index(request $request) { $results = somemodel::filter($request->all())->paginate(); return view('some.view', compact('results')); }
Comments
Post a Comment