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

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 14, 2025

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

      May 14, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 14, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 14, 2025

      I test a lot of AI coding tools, and this stunning new OpenAI release just saved me days of work

      May 14, 2025

      How to use your Android phone as a webcam when your laptop’s default won’t cut it

      May 14, 2025

      The 5 most customizable Linux desktop environments – when you want it your way

      May 14, 2025

      Gen AI use at work saps our motivation even as it boosts productivity, new research shows

      May 14, 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

      Strategic Cloud Partner: Key to Business Success, Not Just Tech

      May 14, 2025
      Recent

      Strategic Cloud Partner: Key to Business Success, Not Just Tech

      May 14, 2025

      Perficient’s “What If? So What?” Podcast Wins Gold at the 2025 Hermes Creative Awards

      May 14, 2025

      PIM for Azure Resources

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

      Windows 11 24H2’s Settings now bundles FAQs section to tell you more about your system

      May 14, 2025
      Recent

      Windows 11 24H2’s Settings now bundles FAQs section to tell you more about your system

      May 14, 2025

      You can now share an app/browser window with Copilot Vision to help you with different tasks

      May 14, 2025

      Microsoft will gradually retire SharePoint Alerts over the next two years

      May 14, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Preserving Collection Keys in Laravel API Resources

    Preserving Collection Keys in Laravel API Resources

    December 20, 2024

    Preserving Collection Keys in Laravel API Resources

    When building APIs, Laravel reindexes resource collections numerically by default. For cases where original keys carry meaning, preserveKeys property maintains the intended data structure.

    <?php
    
    namespace AppHttpResources;
    
    use IlluminateHttpResourcesJsonJsonResource;
    
    class KeyValueResource extends JsonResource
    {
        public $preserveKeys = true; // [tl! focus]
    
        public function toArray($request)
        {
            return [
                'value' => $this->value,
                'updated_at' => $this->updated_at,
                'metadata' => $this->metadata
            ];
        }
    }
    

    Here is an example on how this might look in your Laravel applications.

    <?php
    
    namespace AppHttpControllers;
    
    use AppModelsSetting;
    use AppHttpResourcesSettingResource;
    
    class SettingController extends Controller
    {
        public function index()
        {
            $settings = Setting::all()->keyBy('key');
            
            return SettingResource::collection($settings);
        }
    }
    
    class SettingResource extends JsonResource
    {
        public $preserveKeys = true;
    
        public function toArray($request)
        {
            return [
                'value' => $this->formatValue(),
                'type' => $this->type,
                'last_updated' => $this->updated_at->toDateTimeString(),
                'editable' => $this->is_editable
            ];
        }
    }
    

    Then, it would give you a response like this:

    {
        "data": {
            "app_name": {
                "value": "My Application",
                "type": "string",
                "last_updated": "2024-03-15 10:30:00",
                "editable": true
            },
            "max_upload_size": {
                "value": 10485760,
                "type": "integer",
                "last_updated": "2024-03-15 10:30:00",
                "editable": true
            }
        }
    }
    

    The preserveKeys property ensures meaningful keys are maintained in your API responses, particularly valuable for configuration data and key-value structures.


    The post Preserving Collection Keys in Laravel API Resources 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 ArticleWorking With URIs in Laravel
    Next Article stepanenko3/laravel-helpers

    Related Posts

    Development

    February 2025 Baseline monthly digest

    May 14, 2025
    Development

    Top Ways Hackers Exploit Web Applications (and How to Prevent Them)

    May 14, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    College grads with AI experience attract employers from every job sector

    Development

    Finding leaked passwords with AI: How we built Copilot secret scanning

    News & Updates

    Microsoft: Windows 11 March 2025 update bug deletes Copilot app, unpins from the taskbar

    Operating Systems

    You can now speak to Microsoft’s Copilot Voice in 40 languages, for free

    News & Updates

    Highlights

    CVE-2025-43918 – SSL.com Domain Validation Email Spoofing Vulnerability

    April 20, 2025

    CVE ID : CVE-2025-43918

    Published : April 19, 2025, 10:15 p.m. | 1 day ago

    Description : SSL.com before 2025-04-19, when domain validation method 3.2.2.4.14 is used, processes certificate requests such that a trusted TLS certificate may be issued for the domain name of a requester’s email address, even when the requester does not otherwise establish administrative control of that domain.

    Severity: 6.4 | MEDIUM

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    CVE-2025-20973 – Android Secure Folder Authentication Bypass

    May 7, 2025

    Alibaba Researchers Propose VideoLLaMA 3: An Advanced Multimodal Foundation Model for Image and Video Understanding

    January 26, 2025

    CVE-2025-4449 – D-Link DIR-619L Remote Buffer Overflow Vulnerability

    May 9, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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