Created
April 1, 2014 07:34
-
-
Save duikb00t/9909538 to your computer and use it in GitHub Desktop.
controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SearchController extends \BaseController { | |
public function appendValue($data, $type, $element) | |
{ | |
// operate on the item passed by reference, adding the element and type | |
foreach ($data as $key => & $item) { | |
$item[$element] = $type; | |
} | |
return $data; | |
} | |
public function appendURL($data, $prefix) | |
{ | |
// operate on the item passed by reference, adding the url based on slug | |
foreach ($data as $key => & $item) { | |
$item['url'] = $item; | |
} | |
return $data; | |
} | |
/** | |
* Display a listing of the resource. | |
* | |
* @return Response | |
*/ | |
public function index() | |
{ | |
// Receive the data | |
$query = e(Input::get('q','')); | |
if(!$query && $query == '') return Response::json(array(), 400); | |
// Select on differen tables. | |
$users = User::select(array('id','role','email','first_name','last_name')) | |
->where('first_name','like','%'.$query.'%') | |
->orWhere('last_name','like','%'.$query.'%') | |
->orWhere('email','like','%'.$query.'%') | |
->orWhere('role','like','%'.$query.'%') | |
->get(array('first_name','last_name')) | |
->toArray(); | |
// Data normalization :: | |
//$users = $this->appendURL($users, 'users'); | |
// Add type of data to each line of each set of results. | |
$users = $this->appendValue($users,'user','class'); | |
// Merge all data into one way | |
$data = array_merge($users); | |
return Response::json(array( | |
'data'=>$data | |
)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment