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

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 1, 2025

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

      June 1, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 1, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 1, 2025

      My top 5 must-play PC games for the second half of 2025 — Will they live up to the hype?

      June 1, 2025

      A week of hell with my Windows 11 PC really makes me appreciate the simplicity of Google’s Chromebook laptops

      June 1, 2025

      Elden Ring Nightreign Night Aspect: How to beat Heolstor the Nightlord, the final boss

      June 1, 2025

      New Xbox games launching this week, from June 2 through June 8 — Zenless Zone Zero finally comes to Xbox

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

      Student Record Android App using SQLite

      June 1, 2025
      Recent

      Student Record Android App using SQLite

      June 1, 2025

      When Array uses less memory than Uint8Array (in V8)

      June 1, 2025

      Laravel 12 Starter Kits: Definite Guide Which to Choose

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

      My top 5 must-play PC games for the second half of 2025 — Will they live up to the hype?

      June 1, 2025
      Recent

      My top 5 must-play PC games for the second half of 2025 — Will they live up to the hype?

      June 1, 2025

      A week of hell with my Windows 11 PC really makes me appreciate the simplicity of Google’s Chromebook laptops

      June 1, 2025

      Elden Ring Nightreign Night Aspect: How to beat Heolstor the Nightlord, the final boss

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

    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

    Security

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

    June 2, 2025
    Security

    Google AI Edge Gallery: Unleash On-Device AI Power on Your Android (and Soon iOS!)

    June 2, 2025
    Leave A Reply Cancel Reply

    Hostinger

    Continue Reading

    10 Best Free and Open Source Linux Document Processors

    Linux

    The Power of Community in Creative Journeys

    Development

    A Beginner’s Perspective on Generative AI

    Development

    Former OpenAI Chief Scientist starts new AI firm with a keen focus on building safe superintelligence over its rival’s priority on ‘shiny products’

    Development

    Highlights

    This rugged smartphone has a highly-functional feature that made my iPhone look bad

    April 7, 2025

    Thanks to the large battery size and a slew of added functions, the Blackview BV7300…

    How To Write Test Cases For Radio Button

    August 3, 2024

    Is your Chromecast V2 broken? Don’t worry, Google is going to (slowly) fix it

    March 17, 2025

    CVE-2025-37984 – Linux Kernel ECDsa Integer Overflow Vulnerability

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

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