<?php
class QueriesController extends BaseController {
public function init()
{
$page = Input::exists('page') ? Input::get('page') : 1;
$perpage = Input::exists('per_page') ? (Input::get('per_page') > 50) ? 50 : Input::get('per_page') : 18;
$sort = Input::exists('sort') ? Input::get('sort') : 'created_at';
$sdir = Input::exists('sdir') ? Input::get('sdir') : 'desc';
$tags = Input::exists('tags') ? (explode(',', Input::get('tags'))) : null;
$providers = Input::exists('providers') ? (explode(',', Input::get('providers'))) : null;
$link = new Link;
if($tags && !$providers)
{
$links = $link->whereHas('tags', function($q) use ($tags) {
$q->whereIn('text', $tags);
});
}
if($providers && !$tags)
{
$links = $link::whereIn('provider_name', $providers);
}
if($tags && $providers)
{
$links = $link->whereIn('provider_name', $providers)->whereHas('tags', function($q) use ($tags) {
$q->whereIn('text', $tags);
});
}
// this, surprisingly, works
// $count = $links->count();
// $load = $links->get()->load('tags');
// $array = $load->toArray();
// return Paginator::make($array, $count, $perpage);
// now, how to make it automagically work for the built in paging?
return $links->load('tags')->orderBy($sort, $sdir)->simplePaginate($perpage);
}
}