Skip to main content

Endpoint

PUT /articles/{id}
This endpoint requires authentication with a valid JWT token AND Admin role.

Description

Updates an existing article by ID. This endpoint allows administrators to modify article details and replace associated images. The endpoint:
  • Validates all input data according to business rules
  • Updates the article record in a database transaction
  • Replaces existing images with new ones if provided
  • Returns the updated article with all relationships
When images are provided in the update request, ALL existing images are deleted and replaced with the new ones. To keep existing images, don’t include the images field in the request.

Authentication

Authorization
string
required
Bearer token for authentication. Format: Bearer YOUR_JWT_TOKEN
User must have the Admin role. Regular users will receive a 403 Forbidden response.

Headers

Content-Type
string
required
Must be multipart/form-data when uploading images, or application/json for data only
Accept-Language
string
default:"en"
Language preference for response messages. Supported values: en, es
Accept
string
default:"application/json"
Response content type

Path Parameters

id
integer
required
The unique ID of the article to update

Request Body Parameters

name
string
required
Name of the articleValidation Rules:
  • Required
  • Must be a string
  • Minimum 3 characters
description
string
required
Detailed description of the articleValidation Rules:
  • Required
  • Must be a string
  • Minimum 10 characters
  • Maximum 255 characters
price
number
required
Price of the articleValidation Rules:
  • Required
  • Must be numeric
  • Minimum value: 0
category_id
integer
required
ID of the category this article belongs toValidation Rules:
  • Required
  • Must exist in the categories table
subcategory_id
integer
required
ID of the subcategory this article belongs toValidation Rules:
  • Required
  • Must exist in the subcategories table
images
array
Array of image files to upload (will replace ALL existing images)Validation Rules:
  • Optional (nullable)
  • Must be an array if provided
  • Each image must meet the following criteria:
    • Must be a valid image file
    • Allowed formats: jpeg, png, jpg
    • No maximum size limit (removed in update validation)

Response

Success Response (200 OK)

success
boolean
required
Always true for successful requests
message
string
required
Success message indicating the article was updated
data
object
required

Example Request

# Update with images
curl -X PUT https://api.example.com/articles/1 \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Accept: application/json" \
  -H "Accept-Language: en" \
  -F "name=Gaming Laptop RTX 4080" \
  -F "description=Updated high-performance gaming laptop with RTX 4080 graphics card" \
  -F "price=1799.99" \
  -F "category_id=2" \
  -F "subcategory_id=5" \
  -F "images[]=@/path/to/new-laptop.jpg"

# Update without images (keeps existing)
curl -X PUT https://api.example.com/articles/1 \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{
    "name": "Gaming Laptop RTX 4080",
    "description": "Updated high-performance gaming laptop with RTX 4080 graphics card",
    "price": 1799.99,
    "category_id": 2,
    "subcategory_id": 5
  }'

Example Response

Success (200 OK)

{
  "success": true,
  "message": "Article updated successfully",
  "data": {
    "article": {
      "id": 1,
      "category_id": 2,
      "subcategory_id": 5,
      "name": "Gaming Laptop RTX 4080",
      "description": "Updated high-performance gaming laptop with RTX 4080 graphics card",
      "price": 1799.99,
      "images": [
        {
          "id": 3,
          "path": "articles/article_image_1_1709912500.jpg",
          "imageable_type": "App\\Models\\Article",
          "imageable_id": 1
        }
      ]
    }
  }
}

Error Responses

401 - Unauthorized

{
  "success": false,
  "message": "Unauthenticated",
  "errors": null
}

403 - Forbidden (Not Admin)

{
  "success": false,
  "message": "Forbidden. Admin role required",
  "errors": null
}

404 - Not Found

{
  "success": false,
  "message": "Article not found",
  "errors": null
}

422 - Validation Error

{
  "success": false,
  "message": "The given data was invalid",
  "errors": {
    "name": ["The name must be at least 3 characters."],
    "price": ["The price must be a number."],
    "category_id": ["The selected category is invalid."]
  }
}

500 - Internal Server Error

{
  "success": false,
  "message": "Article update failed",
  "errors": {
    "exception": "Database transaction failed"
  }
}

Implementation Notes

  • Uses database transactions to ensure atomicity
  • When new images are uploaded:
    1. All existing images are deleted from storage
    2. All existing image records are deleted from database
    3. New images are uploaded and associated with the article
  • If no images are provided, existing images remain unchanged
  • Laravel’s route model binding automatically handles 404 for non-existent article IDs
  • Old image files are permanently deleted from storage when replaced
  • Update validation is similar to create but without the 2MB size limit on images
Important: Providing the images field will DELETE all existing images and replace them with the new ones. To keep existing images, omit the images field from your request entirely.
Source Code Reference: ArticleController::update() at /home/daytona/workspace/source/app/Http/Controllers/ArticleController.php:226Validation Rules: UpdateArticleRequest at /home/daytona/workspace/source/app/Http/Requests/ArticleRequest/UpdateArticleRequest.php:24

Build docs developers (and LLMs) love