Skip to content

Instantly share code, notes, and snippets.

@duikb00t
Created April 1, 2014 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duikb00t/9909538 to your computer and use it in GitHub Desktop.
Save duikb00t/9909538 to your computer and use it in GitHub Desktop.
controller
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