/**
  * Retrieve subcategory by category id request via AJAX.
  *
  * @param Request $request
  * @param $id
  * @return json
  */
 public function subcategories(Request $request, $id)
 {
     if ($request->ajax()) {
         $category = $this->category->findOrFail($id);
         return $category->subcategories;
     } else {
         abort(403, 'Resources are restricted.');
     }
 }
 /**
  * Show the form for editing the specified article.
  *
  * @param $slug
  * @return \Illuminate\Http\Response
  */
 public function edit($slug)
 {
     /*
      * --------------------------------------------------------------------------
      * Show edit form
      * --------------------------------------------------------------------------
      * Populate category into list for build drop down, and try catch old
      * subcategory input because it depends on category, by default subcategory
      * should be exist on edit because all article should have it.
      */
     $article = Article::whereSlug($slug)->firstOrFail();
     $categories = Category::pluck('category', 'id');
     $subcategories = null;
     if (Input::old('category', '') != '') {
         $subcategories = Category::findOrFail(Input::old('category'))->subcategories;
     } else {
         $subcategories = Subcategory::whereCategoryId($article->subcategory->category->id)->get();
     }
     return view('contributor.article_edit', compact('article', 'categories', 'subcategories'));
 }
 /**
  * Remove the specified category from storage.
  *
  * @param Request $request
  * @param  int $id
  * @return \Illuminate\Http\Response
  */
 public function destroy(Request $request, $id = null)
 {
     /*
      * --------------------------------------------------------------------------
      * Delete category
      * --------------------------------------------------------------------------
      * Check if selected variable is not empty so user intends to select multiple
      * rows at once, and prepare the feedback message according the type of
      * deletion action.
      */
     if (!empty(trim($request->input('selected')))) {
         $delete = DB::transaction(function () use($request) {
             try {
                 $category_ids = explode(',', $request->input('selected'));
                 $delete_subcategory = 0;
                 if ($request->input('selected_sub') != '') {
                     $subcategory_ids = explode(',', $request->input('selected_sub'));
                     $delete_subcategory = Subcategory::whereIn('id', $subcategory_ids)->delete();
                 }
                 $delete = Category::whereIn('id', $category_ids)->delete();
                 return $delete + $delete_subcategory;
             } catch (\Exception $e) {
                 return redirect()->back()->withErrors(['error' => Lang::get('alert.error.transaction')])->withInput();
             }
         });
         $message = Lang::get('alert.category.delete_all', ['count' => $delete]);
     } else {
         $category = Category::findOrFail($id);
         $message = Lang::get('alert.category.delete', ['category' => $category->category]);
         $delete = $category->delete();
     }
     if ($delete instanceof RedirectResponse) {
         return $delete;
     }
     if ($delete) {
         return redirect(route('admin.category.index'))->with(['status' => 'warning', 'message' => $message]);
     } else {
         return redirect()->back()->withErrors(['error' => Lang::get('alert.error.database')]);
     }
 }