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

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 3, 2025

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

      June 3, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 3, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 3, 2025

      SteelSeries reveals new Arctis Nova 3 Wireless headset series for Xbox, PlayStation, Nintendo Switch, and PC

      June 3, 2025

      The Witcher 4 looks absolutely amazing in UE5 technical presentation at State of Unreal 2025

      June 3, 2025

      Razer’s having another go at making it so you never have to charge your wireless gaming mouse, and this time it might have nailed it

      June 3, 2025

      Alienware’s rumored laptop could be the first to feature NVIDIA’s revolutionary Arm-based APU

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

      easy-live2d – About Make your Live2D as easy to control as a pixi sprite! Live2D Web SDK based on Pixi.js.

      June 3, 2025
      Recent

      easy-live2d – About Make your Live2D as easy to control as a pixi sprite! Live2D Web SDK based on Pixi.js.

      June 3, 2025

      From Kitchen To Conversion

      June 3, 2025

      Perficient Included in Forrester’s AI Technical Services Landscape, Q2 2025

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

      SteelSeries reveals new Arctis Nova 3 Wireless headset series for Xbox, PlayStation, Nintendo Switch, and PC

      June 3, 2025
      Recent

      SteelSeries reveals new Arctis Nova 3 Wireless headset series for Xbox, PlayStation, Nintendo Switch, and PC

      June 3, 2025

      The Witcher 4 looks absolutely amazing in UE5 technical presentation at State of Unreal 2025

      June 3, 2025

      Razer’s having another go at making it so you never have to charge your wireless gaming mouse, and this time it might have nailed it

      June 3, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Dummy – Generate PHP class instances populated with dummy data using Faker

    Dummy – Generate PHP class instances populated with dummy data using Faker

    January 7, 2025

    Dummy - Generate PHP class instances populated with dummy data using Faker

    Dummy is a package created by Steve Bauman that can be used to generate PHP class instances populated with dummy data using Faker. This can be really useful in generating instances of any class (not just Eloquent models) with realistic data during testing, when seeding databases or for rapid prototyping.

    You can install the package via composer:

    composer require directorytree/dummy
    

    Dummy allows you to generate classes with dummy data in two ways:

    1. Using the HasFactory Trait directly on the class you would like to generate dummy instances of.
    2. Using a separate Factory class, which can give you more control over the dummy data generation process.

    Examples of each option can be seen below:

    namespace AppData;
    
    use FakerGenerator;
    use DirectoryTreeDummyHasFactory;
    
    class Player
    {
        use HasFactory;
        
        /**
         * Create a new player instance.
         */
        public function __construct(
            public string $name,
            public DateTime $dob,
            public string $nationality,
        ) {}
        
        /**
         * Define the factory's default state.
         */
        protected function getFactoryDefinition(Generator $faker): array
        {
            return [
                'name' => $faker->name(),
                'dob' => $faker->dateTimeBetween('1970-01-01', '-16 years'),
                'nationality' => $faker->country(),
            ];
        }
        
        /**
         * Create a new instance of the class using the factory definition.
         */
        protected static function toFactoryInstance(array $attributes): static
        {
            return new static(
                $attributes['name'],
                $attributes['dob'],
                $attributes['nationality'],
            );
        }
    }
    
    namespace AppFactories;
    
    use AppDataPlayer;
    use DirectoryTreeDummyFactory;
    
    class PlayerFactory extends Factory
    {
        /**
         * Define the factory's default state.
         */
        protected function definition(): array
        {
            return [
                'name' => $this->faker->name(),
                'dob' => $this->faker->dateTimeBetween('1970-01-01', '-16 years'),
                'nationality' => $this->faker->country(),
            ];
        }
        
        /**
         * Generate a new instance of the class.
         */
        protected function generate(array $attributes): Player
        {
            return new Player(
                $attributes['name'],
                $attributes['dob'],
                $attributes['nationality'],
            );
        }
    }
    

    You can then generate dummy instances of your class using the make method

    // Using the trait:
    $player = Player::factory()->make();
    
    // Using the factory class:
    $player = PlayerFactory::new()->make();
    

    The package also allows you to:

    Hostinger
    • Override attributes in your definition
    • Generate multiple instances using the count method.
    • Allow you to define specific methods to apply modifications of default attributes using Factory states
    • Create factory callbacks to perform additional tasks after making a class.
    • Use factory sequences to alternate the value of a given attribute for each generated class
    • Customize the Collection instances returned when making more than one dummy class.

    So in a nutshell, Dummy can complement Laravel’s default factory classes by extending support to non-Eloquent classes, offering both trait-based and separate factory class implementations, and providing a fluent API for instance generation. This makes it a versatile tool for generating dummy data across different parts of your application.

    Learn more about this package and view the source code on Github.


    The post Dummy – Generate PHP class instances populated with dummy data using Faker 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 

    Hostinger
    Facebook Twitter Reddit Email Copy Link
    Previous ArticleNew low-code solution Qt Accelerate speeds up product development for industrial and consumer electronics use cases
    Next Article Optimizing Large Data Delivery with Laravel Streaming Responses

    Related Posts

    Development

    The Open Source LLM Agent Handbook: How to Automate Complex Tasks with LangGraph and CrewAI

    June 3, 2025
    Artificial Intelligence

    Markus Buehler receives 2025 Washington Award

    June 3, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Save $260 on Amazon’s 75-inch Omni Series Fire TV this Memorial Day

    Development

    Online Cloth Rental System using PHP and MySQL

    Development

    Koteshen – Wallet opening invoicing tool for web agencies

    Development

    Admins will soon be able to monitor the updates of the Microsoft Teams clients across the organization

    Operating Systems

    Highlights

    Development

    Vsqared Ventures unveils €214M for European deeptech

    June 11, 2024

    Vsquared Ventures has closed largest European early-stage deeptech fund at €214M. The funding will be…

    Dispel Appoints Dean Macris as Chief Information Security Officer

    May 17, 2024

    CVE-2025-27701 – Apache HTTP Server Null Pointer Dereference

    May 27, 2025

    Meet Sailor: A Family of Open Language Models Ranging from 0.5B to 7B Parameters for Southeast Asian (SEA) Languages

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

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