public function store(Request $request)
    {
        // check if user has permissions to access this link
        if(!User::hasAccess('Categories', 'create')) {
            return $this->noPermission();
        }

        $data = $request->all();
        $structuredData =  $this->generateTitleAndSlug($data['form']);
        $form = $structuredData['form'];
        $errors = $structuredData['errors'];

        // return errors if there are any
        if(count($errors)) {
            return $this->response("From errors", 400, null, false, false, true, $errors);
        }

        // if category is being created
        if(!isset($form['categoryID'])) {
            // Order item
            $catCount = Category::where("postTypeID", $request->postTypeID)->count();
            $order = $catCount+1;
            $categoryModel = new Category();
        }else{
            // if category is being updated
            $categoryModel = Category::find($form['categoryID']);
            if(!$categoryModel) {
                return $this->response("Category doesn't exist", 500);
            }
            $order = $categoryModel->order;
            $oldTitle = $categoryModel->setAutoTranslate(false)->title;
        }

        // parent ID
        $parentID = 0;
        if(isset($form['parent']['categoryID'])) {
            $parentID = $form['parent']['categoryID'];
        }

        // Create Category
        $categoryModel->title = $form['title'];
        $categoryModel->postTypeID = $data['postTypeID'];
        $categoryModel->parentID = ($parentID ? $parentID: null);
        $categoryModel->featuredImageID = $form['featuredImageID'];
        $categoryModel->description = $form['description'];
        $categoryModel->slug = $form['slug'];
        $categoryModel->isVisible = $form['isVisible'];
        $categoryModel->order = $order;
        $categoryModel->createdByUserID = Auth::user()->userID;
        $categoryModel->customFields = $request->customFieldValues;

        // return results
        if ($categoryModel->save()) {
            if(isset($form['categoryID'])) {
                // update label in menu link with the category title
                $this->updateMenuLinkLabel($data['id'], $form['title'], $oldTitle);
            }
            $redirect = $this->redirect($request->redirect, $categoryModel->categoryID, $data['postTypeID']);
            $response = $this->response('Category is saved', 200, $redirect['id'], $redirect['view'], $redirect['url']);
        }else{
            $response = $this->response('Category is not saved. Please try again later!', 500);
        }
        return $response;
    }
public function store(Request $request)
    {
        // check if user has permissions to access this link
        if(!User::hasAccess('Categories', 'create')) {
            return $this->noPermission();
        }

        $data = $request->all();
        $structuredData =  $this->generateTitleAndSlug($data['form']);
        $form = $structuredData['form'];
        $errors = $structuredData['errors'];

        // return errors if there are any
        if(count($errors)) {
            return $this->response("From errors", 400, null, false, false, true, $errors);
        }

        // if category is being created
        if(!isset($form['categoryID'])) {
            // Order item
            $catCount = Category::where("postTypeID", $request->postTypeID)->count();
            $order = $catCount+1;
            $categoryModel = new Category();
        }else{
            // if category is being updated
            $categoryModel = Category::find($form['categoryID']);
            if(!$categoryModel) {
                return $this->response("Category doesn't exist", 500);
            }
            $order = $categoryModel->order;
            $oldTitle = $categoryModel->setAutoTranslate(false)->title;
        }

        // parent ID
        $parentID = 0;
        if(isset($form['parent']['categoryID'])) {
            $parentID = $form['parent']['categoryID'];
        }

        // Create Category
        $categoryModel->title = $form['title'];
        $categoryModel->postTypeID = $data['postTypeID'];
        $categoryModel->parentID = ($parentID ? $parentID: null);
        $categoryModel->featuredImageID = $form['featuredImageID'];
        $categoryModel->description = $form['description'];
        $categoryModel->slug = $form['slug'];
        $categoryModel->isVisible = $form['isVisible'];
        $categoryModel->order = $order;
        $categoryModel->createdByUserID = Auth::user()->userID;
        $categoryModel->customFields = $request->customFieldValues;

        // return results
        if ($categoryModel->save()) {
            if(isset($form['categoryID'])) {
                // update label in menu link with the category title
                $this->updateMenuLinkLabel($data['id'], $form['title'], $oldTitle);
            }
            $redirect = $this->redirect($request->redirect, $categoryModel->categoryID, $data['postTypeID']);
            $response = $this->response('Category is saved', 200, $redirect['id'], $redirect['view'], $redirect['url']);
        }else{
            $response = $this->response('Category is not saved. Please try again later!', 500);
        }
        return $response;
    }
public function store ( Request $ request ) { // check if user has permissions to access this link if ( ! User :: hasAccess ( 'Categories' , 'create' ) ) { return $ this -> noPermission ( ) ; } $ data = $ request -> all ( ) ; $ structuredData = $ this -> generateTitleAndSlug ( $ data [ 'form' ] ) ; $ form = $ structuredData [ 'form' ] ; $ errors = $ structuredData [ 'errors' ] ; // return errors if there are any if ( count ( $ errors ) ) { return $ this -> response ( "From errors" , 400 , null , false , false , true , $ errors ) ; } // if category is being created if ( ! isset ( $ form [ 'categoryID' ] ) ) { // Order item $ catCount = Category :: where ( "postTypeID" , $ request -> postTypeID ) -> count ( ) ; $ order = $ catCount + 1 ; $ categoryModel = new Category ( ) ; } else { // if category is being updated $ categoryModel = Category :: find ( $ form [ 'categoryID' ] ) ; if ( ! $ categoryModel ) { return $ this -> response ( "Category doesn't exist" , 500 ) ; } $ order = $ categoryModel -> order ; $ oldTitle = $ categoryModel -> setAutoTranslate ( false ) -> title ; } // parent ID $ parentID = 0 ; if ( isset ( $ form [ 'parent' ] [ 'categoryID' ] ) ) { $ parentID = $ form [ 'parent' ] [ 'categoryID' ] ; } // Create Category $ categoryModel -> title = $ form [ 'title' ] ; $ categoryModel -> postTypeID = $ data [ 'postTypeID' ] ; $ categoryModel -> parentID = ( $ parentID ? $ parentID : null ) ; $ categoryModel -> featuredImageID = $ form [ 'featuredImageID' ] ; $ categoryModel -> description = $ form [ 'description' ] ; $ categoryModel -> slug = $ form [ 'slug' ] ; $ categoryModel -> isVisible = $ form [ 'isVisible' ] ; $ categoryModel -> order = $ order ; $ categoryModel -> createdByUserID = Auth :: user ( ) -> userID ; $ categoryModel -> customFields = $ request -> customFieldValues ; // return results if ( $ categoryModel -> save ( ) ) { if ( isset ( $ form [ 'categoryID' ] ) ) { // update label in menu link with the category title $ this -> updateMenuLinkLabel ( $ data [ 'id' ] , $ form [ 'title' ] , $ oldTitle ) ; } $ redirect = $ this -> redirect ( $ request -> redirect , $ categoryModel -> categoryID , $ data [ 'postTypeID' ] ) ; $ response = $ this -> response ( 'Category is saved' , 200 , $ redirect [ 'id' ] , $ redirect [ 'view' ] , $ redirect [ 'url' ] ) ; } else { $ response = $ this -> response ( 'Category is not saved. Please try again later!' , 500 ) ; } return $ response ; }
Save Category in database.

@param  Request $request
@return array
@throws \Exception
Save Category in database .