Hello there, How can filtering data of nested and many relations tables on this example
"item_id" => ""
"updated_date_from" => ""
"updated_date_to" => ""
"title" => ""
"childes_no_from" => ""
"childes_no_to" => ""
"seeds_no_from" => ""
"seeds_no_to" => ""
"feeds_no_from" => ""
"feeds_no_to" => ""
"status" => ""
public function ajax(Request $request){
$categories = new Category();
if (Input::has('action')){
$action = Input::get('action');
if ($action == 'filter'){
if(Input::has('item_id')){
$categories = $categories->where('id', '=', Input::get('item_id'));
}
if(Input::has('updated_date_from')){
$categories = $categories->where('updated_at', '>', Carbon::createFromFormat('d/m/Y',Input::get('updated_date_from')));
}
if(Input::has('updated_date_to')){
$categories = $categories->where('updated_at', '<', Carbon::createFromFormat('d/m/Y',Input::get('updated_date_to')));
}
if(Input::has('title')){
$categories = $categories->where('title', 'LIKE', '%'.Input::get('title').'%');
}
if(Input::has('status')){
$categories = $categories->where('status', '=', Input::get('status'));
}
// childes_no_from
// childes_no_to
// seeds_no_from
// seeds_no_to
// feeds_no_from
// feeds_no_to
}
}
$categories = $categories->orderBy($this->columns_list[$_REQUEST['order'][0]['column']], $_REQUEST['order'][0]['dir'])
->limit($_REQUEST['length'])->offset($_REQUEST['start'])
->get();
$categories_count = Category::all()->count();
$sEcho = intval($_REQUEST['draw']);
$records = array();
$records["data"] = array();
foreach ($categories as $category) {
$records["data"][] = array(
'<label class="mt-checkbox mt-checkbox-single mt-checkbox-outline"><input name="id[]" type="checkbox" class="checkboxes" value="'.$category['id'].'"/><span></span></label>',
$category['id'],
isset($category['updated_at']) ? $category->updated_at->diffForHumans() : $category->created_at->diffForHumans() ,
($category->parent['title'] ) ? $category->parent['title'].' - '.$category['title'] : $category['title'],
$category->children()->count(),
$category->seeds()->count(),
$category->feeds()->count(),
'<span class="label label-sm label-'.key($this->status_list[$category['status']]).'">'.
current($this->status_list[$category['status']]).
'</span>',
'<a href="'.route('categories.show',['id'=>$category['id']]).'" class="btn btn-sm btn-outline btn-success "><i class="fa fa-search"></i> Show</a>'
.'<a href="'.route('categories.edit',['id'=>$category['id']]).'" class="btn btn-sm btn-outline btn-warning "><i class="fa fa-pencil-square-o"></i> Edit</a>'
.'<a href="'.route('categories.destroy',['id'=>$category['id']]).'" class="btn btn-sm btn-outline btn-danger "><i class="fa fa-times"></i> Delete</a>'
); // row data
}
$records["draw"] = $sEcho;
$records["recordsTotal"] = $categories_count;
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'filter') {
$records["recordsFiltered"] = $categories->count();
}else{
$records["recordsFiltered"] = $categories_count;
}
return json_encode($records);
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community