laravel - PHP Search function string to number conversion (search for month) -
in laravel, using following code in eventscontroller:
public function index() { $search = input::get('search'); $searchresult = events::orderby($order, $by)->where(function ($query) use ($search) { $query->where('name', 'like', '%'. $search .'%') ->orwhere('date', 'like', '%'. $search .'%')->get(); })->paginate(10); $search = str_getcsv($search, ' '); if (in_array("january", $search)) { $searchresult = events::orderby($order, $by)->where('date', 'like', '%2015-01'%')->paginate(10); } if (in_array("february", $search)) { $searchresult = events::orderby($order, $by)->where('date', 'like', '%2015-02'%')->paginate(10); } return view::make('events.index') ->with('events', $searchresult) ->with('search', $search); }
the code working ofcourse bad practice. store dates in database below:
2015-07-30 (y-m-d)
what want accomplish is, when search "january" in search, should show results january (2015-01-xx). same february, , on.
with above code accomplish this, need copy same code 12 months. isn't good. on making code better purpose? other tips or tricks? posting learning purposes.
issue #2. want able search "1 january" example. or advice on how accomplish this?
the easiest way rid of ifs introducing mapping between month names , month number, e.g.:
$month = 'january'; // february, march, ... $monthnumber = date('m', strtotime($month); $searchresult = events::orderby($order, $by)->where('date', 'like', '%2015-' . $monthnuber . '%')->paginate(10);
Comments
Post a Comment