/**
  * @param       $filters
  * @param  null $limit
  *
  * @return \Illuminate\Database\Eloquent\Collection|static[]
  */
 public function getAll($filters, $limit = null)
 {
     $query = $this->post->select('*');
     if (!\Entrust::can('manage-all-content')) {
         $query->where('created_by', auth()->user()->id);
     }
     $from = "posts ";
     if (isset($filters['status']) && $filters['status'] != '') {
         $status = $filters['status'];
         $query->whereRaw("posts.metadata->>'status' = ?", [$status]);
     }
     if (isset($filters['date_from']) && $filters['date_from'] != '') {
         $query->whereRaw("date(created_at) >= ?", [str_replace('/', '-', $filters['date_from'])]);
     }
     if (isset($filters['date_to']) && $filters['date_to'] != '') {
         $query->whereRaw("date(created_at) <= ?", [str_replace('/', '-', $filters['date_to'])]);
     }
     if (isset($filters['post_type']) && $filters['post_type'] != '') {
         $post_type = $filters['post_type'];
         $query->whereRaw("posts.metadata->>'type' = ?", [$post_type]);
     }
     if (array_has($filters, "sub_category1")) {
         $ids = $filters['sub_category1'];
         $query->category($ids);
     }
     if (array_has($filters, "sub_category")) {
         $category = Category::find($filters['sub_category']);
         $category_ids = $category->getDescendantsAndSelf()->lists('id')->toArray();
         $query->category($category_ids);
     }
     if (array_has($filters, "category")) {
         $category = Category::find($filters['category']);
         $category_ids = $category->getDescendantsAndSelf()->lists('id')->toArray();
         $query->category($category_ids);
     }
     $query->from($this->db->raw($from));
     $query->orderBy('updated_at', 'DESC');
     if (is_null($limit)) {
         return $query->get();
     }
     return $query->paginate();
 }
<div class="col-md-3 left_col">
    <div class="left_col scroll-view">
        <div class="navbar nav_title" style="border: 0;">
            <a href="{{route('home')}}" class="site_title"><i class="fa fa-paw"></i> <span>Shuvayatra</span></a>
        </div>

        <div class="clearfix"></div>
        <br/>

        <div class="menu_section">
            <h3>Content</h3>
            <ul class="">
                <?php 
$sections = \App\Nrna\Models\Category::all()->toHierarchy();
?>
                @foreach($sections as $section)
                    <li class="active">
                        <span class="">{{$section->title}}</span> <a class=""
                                                                     href="{{route('category.create')}}?section_id={{$section->id}}">Add</a>
                        <ul class="child_menu">
                            @foreach($section->children as $child)
                                <li>{{$child->title}} <a class="sidebar-edit"
                                                         href="{{route('category.edit',$child->id)}}?section_id={{$section->id}}"
                                                         href="">edit</a></li>
                            @endforeach
                        </ul>
                    </li>
                @endforeach

            </ul>
        </div>
 /**
  * Category by section
  *
  * @param $section
  *
  * @return mixed|static
  */
 public function findBySection($section)
 {
     return $this->category->where('section', $section)->first();
 }
 /**
  * @param Category $category
  *
  * @return mixed
  */
 public function buildCategory(Category $category)
 {
     $categoryArray['id'] = $category->id;
     $categoryArray['title'] = $category->title;
     $categoryArray['alias_name'] = $category->section;
     $categoryArray['parent_alias'] = is_null($category->parent_id) ? null : $category->getRoot()->section;
     $categoryArray['description'] = $category->description;
     $categoryArray['featured_image'] = $category->main_image_link;
     $categoryArray['icon'] = $category->icon_link;
     $categoryArray['small_icon'] = $category->small_icon_link;
     $categoryArray['position'] = $category->position;
     $categoryArray['small_icon'] = $category->small_icon_link;
     $categoryArray['parent_id'] = $category->parent_id;
     $categoryArray['lft'] = $category->lft;
     $categoryArray['rgt'] = $category->rgt;
     $categoryArray['depth'] = $category->depth;
     $categoryArray['created_at'] = $category->created_at->timestamp;
     $categoryArray['updated_at'] = $category->updated_at->timestamp;
     return $categoryArray;
 }
</div>

@if(!request()->has('section_id'))
    <div class="form-group {{ $errors->has('section') ? 'has-error' : ''}}">
        {!! Form::label('section', trans('category.section'), ['class' => 'col-sm-3 control-label']) !!}
        <div class="col-sm-6">
            {!! Form::text('section', null, ['class' => 'form-control']) !!}
            {!! $errors->first('section', '<p class="help-block">:message</p>') !!}
        </div>
    </div>
@endif
{!! Form::hidden('parent_id', request()->get('section_id', null), ['class' => 'form-control']) !!}
<?php 
$main_category = false;
if (request()->has('section_id')) {
    $main_category = \App\Nrna\Models\Category::find(request()->get('section_id'));
}
?>
@if($main_category && $main_category->getLevel()==0)

    <div class="form-group {{ $errors->has('main_image') ? 'has-error' : ''}}">
        {!! Form::label('main_image', 'Main Image: ', ['class' => 'col-sm-3 control-label']) !!}
        <div class="col-sm-6">
            {!! Form::file('main_image', null, ['class' => 'form-control']) !!}
            {!! $errors->first('main_image', '<p class="help-block">:message</p>') !!}
            @if(isset($category))
                <a href="#" class="thumbnail">
                    <img src="{{$category->main_image_link}}">
                </a>
            @endif
        </div>
                <li>
                    <a href="{{route('apilogs.index')}}">Api Log</a>
                </li>

                <li>
                    <a href="{{route('tag.index')}}" class="user-profile dropdown-toggle">Tags</a>
                </li>


                <li>
                    <a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                        Content <span class="caret"></span>
                    </a>
                    <ul class="dropdown-menu">
                        <?php 
$sections = \App\Nrna\Models\Category::roots()->get();
?>
                        @foreach($sections as $section)
                        <li><a href="{{route('post.index')}}?category={{$section->id}}"> {{$section->title}}</a>
                        </li>
                        @endforeach
                        <li><a href="{{route('category.index')}}"> Manage <i class="glyphicon glyphicon-cog pull-right"></i> </a>
                        </li>
                    </ul>
                </li>
                @endrole


                <li>
                    <a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                    <!-- <img class="img-responsive admin-image" src="{{url()}}/images/user.png" alt=""> -->
                            <?php 
$url = route('post.index') . "?" . request()->getQueryString();
?>
                            <a href="{{removeParam($url,['sub_category','sub_category1'])}}&sub_category={{$child->id}}"
                               class="list-group-item @if(request()->has('sub_category')&& request()->get('sub_category') == $child->id) active @endif"
                               data-parent="#sub-menu">{{$child->title}}</a>
                        @endforeach
                    </div>
                </div>
            </div>
            @endif

            @if(request()->has('sub_category'))
                <?php 
$post_column = $post_column - 2;
$sub_category = Category::find(request()->get('sub_category'));
?>
                <div class="col-md-6 col-xs-12 sub-sidebar mCustomScrollbar">
                    <div class="list-group">
                            <span class="list-group-item"><strong>{{$sub_category->title}}</strong><a
                                        class="pull pull-right"
                                        href="{{route('category.create')}}?section_id={{$sub_category->id}}"><i
                                            class="glyphicon glyphicon-plus add-icon"></i>Add</a></span>
                        <?php 
$subCategories = $sub_category->getImmediateDescendants();
$subCategories = $subCategories->sortBy('position');
?>
                        @foreach($subCategories as $child)
                            <a href="{{removeParam($url,'sub_category1')}}&sub_category1={{$child->id}}"
                               class="list-group-item @if(request()->has('sub_category1')&& request()->get('sub_category1') == $child->id) active @endif">{{$child->title}}</a>
                        @endforeach
?>
	@foreach(config('post_type') as $key => $post_type)
		<li class="icon-wrap
		@if($post_type_active==$key) active @endif ">
			<a class="post_type {{$key}}" data-post-type="{{$key}}" href="javascript:;">
				{{$post_type}}</a>
		</li>
	@endforeach
</ul>

<?php 
$tagService = app('App\\Nrna\\Services\\TagService');
$tags = $tagService->getList();
$sectionService = app('App\\Nrna\\Services\\SectionService');
$sections = $sectionService->all();
$categories = \App\Nrna\Models\Category::where('depth', '!=', '0')->lists('title', 'id')->toArray();
$postService = app('App\\Nrna\\Services\\PostService');
$posts = $postService->getAllPosts()->lists('title', 'id')->toArray();
$show_text_type = true;
if (isset($post)) {
    $show_text_type = false;
}
$post_categories = [];
if (request()->has('sub_category1')) {
    $post_categories[] = request()->get('sub_category1');
}
if (request()->has('sub_category')) {
    $post_categories[] = request()->get('sub_category');
}
if (isset($post)) {
    $post_categories = $post->categories->lists('id')->toArray();