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

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 31, 2025

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

      May 31, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 31, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 31, 2025

      Windows 11 version 25H2: Everything you need to know about Microsoft’s next OS release

      May 31, 2025

      Elden Ring Nightreign already has a duos Seamless Co-op mod from the creator of the beloved original, and it’ll be “expanded on in the future”

      May 31, 2025

      I love Elden Ring Nightreign’s weirdest boss — he bargains with you, heals you, and throws tantrums if you ruin his meditation

      May 31, 2025

      How to install SteamOS on ROG Ally and Legion Go Windows gaming handhelds

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

      Oracle Fusion new Product Management Landing Page and AI (25B)

      May 31, 2025
      Recent

      Oracle Fusion new Product Management Landing Page and AI (25B)

      May 31, 2025

      Filament Is Now Running Natively on Mobile

      May 31, 2025

      How Remix is shaking things up

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

      Windows 11 version 25H2: Everything you need to know about Microsoft’s next OS release

      May 31, 2025
      Recent

      Windows 11 version 25H2: Everything you need to know about Microsoft’s next OS release

      May 31, 2025

      Elden Ring Nightreign already has a duos Seamless Co-op mod from the creator of the beloved original, and it’ll be “expanded on in the future”

      May 31, 2025

      I love Elden Ring Nightreign’s weirdest boss — he bargains with you, heals you, and throws tantrums if you ruin his meditation

      May 31, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Immutable Value Objects in PHP and Laravel With the Bags Package

    Immutable Value Objects in PHP and Laravel With the Bags Package

    January 8, 2025

    Immutable Value Objects in PHP and Laravel With the Bags Package

    Bag is a PHP and Laravel package for Immutable Value Objects, inspired by Spatie’s laravel-data package. It can help you create immutable objects to encapsulate your data in a type-safe way with data casts and built-in validation. The package suggests it can be used to replace regular arrays in your code to benefit from type safety.

    use BagBag;
    
    readonly class MyValue extends Bag {
        public function __construct(public string $name, public int $age) {}
    }
    
    $value = MyValue::from([
        'name' => 'Davey Shafik',
        'age' => 40,
    ]);
    
    // Create a new instance with a different value
    $newValue = $value->with(age: 41);
    

    Bag also has an integration with Laravel, using standard Collection and Validation in value objects, as well as Eloquent casts and the ability to inject Bag objects into controllers with validation:

    use BagAttributesLaravelFromRouteParameter;
    use BagBag;
    
    class MyValue extends Bag
    {
        #[FromRouteParameter()]
        public string $id;
    }
    
    // MyValue $value will have the populated `id` route parameter
    class MyController extends Controller {
        public function store(MyValue $value) {
            // $value is a validated MyValue object
        }
    }
    

    This package also has an Artisan command to generate a Bag' using the make:bag` command.

    Main Features

    • Immutable & Strongly typed
    • Value casting — both input and output
    • Collection support
    • Composable — nest Bag value objects and collections
    • Built-in validation

    You can get started with Bag by reading the documentation and the source code is available on GitHub at dshafik/bag.


    The post Immutable Value Objects in PHP and Laravel With the Bags Package 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 ArticleString Manipulation Made Easy with Laravel’s AsStringable Cast
    Next Article How Copilot Vastly Improved My React Development

    Related Posts

    Security

    New Linux Flaws Allow Password Hash Theft via Core Dumps in Ubuntu, RHEL, Fedora

    June 1, 2025
    Security

    Exploit details for max severity Cisco IOS XE flaw now public

    June 1, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Linux Candy: Meme is a fun tool to create memes

    Linux

    Simple Button Design Tips That Make a Big Impact

    Development

    Alibaba AI Research Releases CosyVoice 2: An Improved Streaming Speech Synthesis Model

    Development

    How We Leveraged DHTMLX to Built an Efficient Construction Project Management Solution for Our Client

    Development

    Highlights

    Development

    Microsoft explains why it’s better to use a local account on Windows 11 — perhaps on purpose

    July 8, 2024

    Microsoft has gone back and forth several times on listing steps on how to switch…

    How to build a Connect Four game in HTML, CSS, and Vanilla

    January 4, 2025

    Microsoft shares rare look at radical Windows 11 Start menu designs it explored before settling on the least interesting one of the bunch

    May 13, 2025

    Perficient Included in Two Commerce-Focused IDC Market Glances

    July 11, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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