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

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 2, 2025

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

      June 2, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 2, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 2, 2025

      How Red Hat just quietly, radically transformed enterprise server Linux

      June 2, 2025

      OpenAI wants ChatGPT to be your ‘super assistant’ – what that means

      June 2, 2025

      The best Linux VPNs of 2025: Expert tested and reviewed

      June 2, 2025

      One of my favorite gaming PCs is 60% off right now

      June 2, 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

      `document.currentScript` is more useful than I thought.

      June 2, 2025
      Recent

      `document.currentScript` is more useful than I thought.

      June 2, 2025

      Adobe Sensei and GenAI in Practice for Enterprise CMS

      June 2, 2025

      Over The Air Updates for React Native Apps

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

      You can now open ChatGPT on Windows 11 with Win+C (if you change the Settings)

      June 2, 2025
      Recent

      You can now open ChatGPT on Windows 11 with Win+C (if you change the Settings)

      June 2, 2025

      Microsoft says Copilot can use location to change Outlook’s UI on Android

      June 2, 2025

      TempoMail — Command Line Temporary Email in Linux

      June 2, 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

    Development

    A Beginner’s Guide to Graphs — From Google Maps to Chessboards

    June 2, 2025
    Development

    How to Code Linked Lists with TypeScript: A Handbook for Developers

    June 2, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Secure a generative AI assistant with OWASP Top 10 mitigation

    Machine Learning

    How to Download and Use Qwen 2.5 AI Chat App

    Operating Systems

    CVE-2025-31644 – F5 BIG-IP Command Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Windows 10 KB5053606 fixes SgrmBroker, direct download .msu

    Operating Systems

    Highlights

    CVE-2024-39442 – Sprd SSense Service Missing Permission Check Vulnerability

    May 5, 2025

    CVE ID : CVE-2024-39442

    Published : May 6, 2025, 2:15 a.m. | 1 hour, 19 minutes ago

    Description : In sprd ssense service, there is a possible missing permission check. This could lead to local information disclosure with no additional execution privileges needed.

    Severity: 6.2 | MEDIUM

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

    Distribution Release: NuTyX 25.02.5

    February 21, 2025

    How to Improve Laravel Livewire Components to Implement Better Dynamic Web User Interfaces with React.js, Vue.js, and Tagify

    May 14, 2024

    DOOM: The Dark Ages PC requirements and specs — Can your computer run id Software’s latest shooter?

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

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