app/Api/V1/Controllers/CategoryController.php
/**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request|UpdateCategoryRequest $request
     * @param JWTAuth $auth
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(UpdateCategoryRequest $request, $id)
    {
        $user = $this->authUser();
        if (!$user) {
            return $this->withMessage('Access denied!', 403);
        }
        $category = Category::query()->find($id);
        if (!$category) {
            return $this->notFound('The goods category does not exist.');
        }
        if (Category::query()->where('name', $request->name)->where('id', '!=', $category->id)->first()) {
            return $this->withMessage('Goods Category named \'' . $request->name . '\' already exist', 400);
        }
        $data = array_filter($request->only(
            'name',
            'description',
            'meta_keyword',
            'meta_description',
            'parent_id',
            'display_order'
        ), 'strlen');
        if ($request->has('parent_id')) {
            $parentId = $request->parent_id;
            $parentCategory = Category::query()->find($parentId);
            if ($parentCategory) {
                if ($parentCategory->isDescendantOf($id)) {
                    throw new UpdateResourceFailedException('Sorry, the specified parent category has this category in it\'s line of descent.');
                }
            }
        }
        $data['updated_by'] = $user->id;
        $picture_id = $category->picture_id == null ? 0 : $category->picture_id;
        $data['property'] = AllGoodsController::extractProperties();
        if ($request->hasFile('picture')) {
            $image = $request->file('picture');
            $name = time() . '.' . $image->getClientOriginalExtension();
            $destinationPath = public_path('/images/goods_category');
            $image->move($destinationPath, $name);
            $picture = Picture::query()->updateOrCreate(['id' => $picture_id], [
                'seo_filename' => $name,
                'mime_type' => $image->getClientOriginalExtension()
            ]);
            $data['picture_id'] = $picture->id;
        }
        if ($category->update($data)) {
            return $this->withJson(['status' => 'Goods category updated']);
        } else {
            throw new UpdateResourceFailedException();
        }
    }
public function update ( UpdateCategoryRequest $ request , $ id ) { $ user = $ this -> authUser ( ) ; if ( ! $ user ) { return $ this -> withMessage ( 'Access denied!' , 403 ) ; } $ category = Category :: query ( ) -> find ( $ id ) ; if ( ! $ category ) { return $ this -> notFound ( 'The goods category does not exist.' ) ; } if ( Category :: query ( ) -> where ( 'name' , $ request -> name ) -> where ( 'id' , '!=' , $ category -> id ) -> first ( ) ) { return $ this -> withMessage ( 'Goods Category named \'' . $ request -> name . '\' already exist' , 400 ) ; } $ data = array_filter ( $ request -> only ( 'name' , 'description' , 'meta_keyword' , 'meta_description' , 'parent_id' , 'display_order' ) , 'strlen' ) ; if ( $ request -> has ( 'parent_id' ) ) { $ parentId = $ request -> parent_id ; $ parentCategory = Category :: query ( ) -> find ( $ parentId ) ; if ( $ parentCategory ) { if ( $ parentCategory -> isDescendantOf ( $ id ) ) { throw new UpdateResourceFailedException ( 'Sorry, the specified parent category has this category in it\'s line of descent.' ) ; } } } $ data [ 'updated_by' ] = $ user -> id ; $ picture_id = $ category -> picture_id == null ? 0 : $ category -> picture_id ; $ data [ 'property' ] = AllGoodsController :: extractProperties ( ) ; if ( $ request -> hasFile ( 'picture' ) ) { $ image = $ request -> file ( 'picture' ) ; $ name = time ( ) . '.' . $ image -> getClientOriginalExtension ( ) ; $ destinationPath = public_path ( '/images/goods_category' ) ; $ image -> move ( $ destinationPath , $ name ) ; $ picture = Picture :: query ( ) -> updateOrCreate ( [ 'id' => $ picture_id ] , [ 'seo_filename' => $ name , 'mime_type' => $ image -> getClientOriginalExtension ( ) ] ) ; $ data [ 'picture_id' ] = $ picture -> id ; } if ( $ category -> update ( $ data ) ) { return $ this -> withJson ( [ 'status' => 'Goods category updated' ] ) ; } else { throw new UpdateResourceFailedException ( ) ; } }
Update the specified resource in storage.
Update the specified resource in storage .
//create category details //            if ($request->details) { //                CategoryDetail::updateOrCreate( //                    ['category_id' => $category->id], //                    ['category_details' => $request->details] //                ); //            }