Support the ongoing development of Laravel.io →
Database Eloquent

Hello there, How can filtering data of nested and many relations tables on this example

Models

Category

  • has many nested categories with parent_id
  • has many seeds
  • has many feeds

Seed

  • has many feeds
  • belongs to one category

Feeds

  • belongs to one category
  • belongs to one seed

I'm using datatables JS with filtering form, this give me requists of many things like this

"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" => ""

I'm allready filtering the data ( just categories database table ) but not the related data on feeds and seeds

Here is my method

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);
}
Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.