Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 16, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 16, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 16, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 16, 2025

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025

      Minecraft licensing robbed us of this controversial NFL schedule release video

      May 16, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The power of generators

      May 16, 2025
      Recent

      The power of generators

      May 16, 2025

      Simplify Factory Associations with Laravel’s UseFactory Attribute

      May 16, 2025

      This Week in Laravel: React Native, PhpStorm Junie, and more

      May 16, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025
      Recent

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Using withoutWrapping to flatten API responses

    Using withoutWrapping to flatten API responses

    December 20, 2024

    Using withoutWrapping to flatten API responses

    Laravel’s API resources wrap responses in a ‘data’ key by default. While useful for many scenarios, sometimes a flatter response structure is needed and you can disable resource wrapping like this:

    <?php
    
    namespace AppProviders;
    
    use IlluminateHttpResourcesJsonJsonResource;
    use IlluminateSupportServiceProvider;
    
    class AppServiceProvider extends ServiceProvider
    {
        public function boot(): void
        {
            JsonResource::withoutWrapping();
        }
    }
    

    Here is an example of how this works when you have withoutWrapping:

    <?php
    
    namespace AppHttpResources;
    
    use IlluminateHttpResourcesJsonJsonResource;
    
    class ArticleResource extends JsonResource
    {
        public function toArray($request)
        {
            return [
                'id' => $this->id,
                'title' => $this->title,
                'content' => $this->content,
                'author' => new AuthorResource($this->whenLoaded('author')),
                'metadata' => [
                    'views' => $this->views_count,
                    'likes' => $this->likes_count,
                    'published_at' => $this->published_at
                ]
            ];
        }
    }
    

    Example response with wrapping disabled:

    [
        {
            "id": 1,
            "title": "Laravel Tips",
            "content": "Article content here",
            "author": {
                "id": 1,
                "name": "John Doe",
                "email": "john@example.com"
            },
            "metadata": {
                "views": 150,
                "likes": 42,
                "published_at": "2024-03-15T10:00:00Z"
            }
        }
    ]
    

    This implementation provides a cleaner API structure while maintaining the flexibility to customize response formats based on your application’s needs.

    Resource wrapping can be disabled globally while still maintaining granular control over your API’s response structure, resulting in more intuitive and easier-to-consume endpoints.


    The post Using withoutWrapping to flatten API responses appeared first on Laravel News.

    Join the Laravel Newsletter to get all the latest
    Laravel articles like this directly in your inbox.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleCustomize the Truncation of HTTP Client Request Exceptions
    Next Article Laravel whenLoaded – Performance Optimization via Conditional Relationship Loading

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 17, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2024-47893 – VMware GPU Firmware Memory Disclosure

    May 17, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    CVE-2025-43955 – Convertigo TwsCachedXPathAPI Commons-JXPath API Deserialization Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Next-Gen UX and UI in Software Solutions

    Development

    Dark Web Actor Advertises New Click Fraud Software for Online Marketing Deception

    Development

    Replicate Laravel PHP Client

    Development

    Highlights

    Development

    Q*: A Versatile Artificial Intelligence AI Approach to Improve LLM Performance in Reasoning Tasks

    June 28, 2024

    Large Language Models (LLMs) have demonstrated remarkable abilities in tackling various reasoning tasks expressed in…

    CodeSOD: Reliability Test

    March 19, 2025

    TopicGPT: A Prompt-based AI Framework that Uses Large Language Models (LLMs) to Uncover Latent Topics in a Text Collection

    June 19, 2024

    Samsung Health is adding these personalized wellness features – and they’re much needed

    January 22, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.