/**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index($cat_id)
 {
     $parent = Category::find($cat_id)->parentCategory()->get();
     $current = Category::find($cat_id);
     $categories = Category::find($cat_id)->subCategories()->get();
     $posts = Category::find($cat_id)->posts()->get();
     return view('user.posts.index', ['posts' => $posts, 'categories' => $categories, 'parent' => $parent, 'current' => $current->toArray()]);
 }
 /**
  * get the specific category and display its posts
  *
  * @return \Illuminate\View\View
  */
 public function getView($category_id)
 {
     $cat = Category::find($category_id);
     $child = Category::where('parent_id', $category_id)->get();
     $states = State::all();
     $posts = null;
     if (count($cat->subCategories->toArray()) > 0) {
         //$posts = DB::select('SELECT * FROM posts WHERE category_id IN (SELECT id FROM categories WHERE parent_id = ? ORDER BY published_at DESC)', [$category_id]);
         $cats = Category::where('parent_id', $category_id)->lists('id');
         $posts = Post::whereIn('category_id', $cats)->orderBy('published_at', 'desc')->paginate(config('traydes.posts_per_page'));
     } else {
         //$posts = $cat->posts;
         $posts = Post::where('category_id', $category_id)->orderBy('published_at', 'desc')->paginate(config('traydes.posts_per_page'));
     }
     return view('index.view', ['posts' => $posts, 'child' => $child, 'states' => $states]);
 }
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     $categories = Category::where('parent_id', 0)->get();
     return view('user.categories.index', ['categories' => $categories]);
 }
 /**
  * displaying the form for new posting
  *
  * @param Request $request
  * @return \Illuminate\View\View
  */
 public function getNew(Request $request)
 {
     $req = $request->get('c');
     $categories = null;
     $template = ['user.posts.templates.index', 'user.posts.templates.form'];
     $index = 0;
     if ($req != null) {
         $category = Category::find($req);
         //            dd($category->subCategories->count());
         if (count($category) > 0) {
             //not empty
             $categories = Category::where('parent_id', '=', $req)->get();
         }
         if ($category->subCategories->count() > 0) {
             $index = 0;
         } else {
             $index = 1;
         }
         return view('user.posts.create', ['categories' => $categories, 'category_id' => (int) $req, 'template' => $template[$index]]);
     }
     $categories = Category::where('parent_id', '=', 0)->get();
     return view('user.posts.create', ['categories' => $categories, 'template' => $template[$index]]);
 }
@include('partials.search')


{{--filter by categories--}}
<div class="panel panel-default">
    <div class="panel-heading">Filter by Category</div>

    <div class="list-group">

        <?php 
$categories = \Traydes\Category::where('parent_id', 0)->get();
?>
        @if(!empty($categories))
            @foreach($categories as $category)
                <a href="{{ url('t/view/'.$category->id) }}" class="list-group-item @if(Request::is('t/view/'.$category->id)) active @endif">{{ $category->name }}</a>

                @if(!empty($category->subCategories()) && Request::is('t/view/'.$category->id))
                    @foreach($category->subCategories as $sub)
                        <a href="{{ url('t/view/'.$sub->id) }}" class="list-group-item @if(Request::is('t/view/'.$sub->id)) active @endif">{{ $sub->name }}</a>
                    @endforeach
                @endif
            @endforeach
        @endif

    </div>

</div>


{{--filter by states--}}
 /**
  * get the category object in json format
  *
  * @param $id
  * @return mixed
  */
 public function getApiCategory($id)
 {
     $category = Category::find($id);
     return $category;
 }