<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mayank-Addweb on Medium]]></title>
        <description><![CDATA[Stories by Mayank-Addweb on Medium]]></description>
        <link>https://medium.com/@mayank.g_75387?source=rss-491a93312824------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ZIKni_KiydQ_nKOBbohunA.jpeg</url>
            <title>Stories by Mayank-Addweb on Medium</title>
            <link>https://medium.com/@mayank.g_75387?source=rss-491a93312824------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 03:06:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mayank.g_75387/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Enhancing Laravel Code Quality with Larastan]]></title>
            <link>https://medium.com/addweb-engineering/enhancing-laravel-code-quality-with-larastan-c7d177160890?source=rss-491a93312824------2</link>
            <guid isPermaLink="false">https://medium.com/p/c7d177160890</guid>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[laravel-developer]]></category>
            <category><![CDATA[tips-laravel]]></category>
            <category><![CDATA[larastan]]></category>
            <dc:creator><![CDATA[Mayank-Addweb]]></dc:creator>
            <pubDate>Mon, 06 Jul 2026 11:07:54 GMT</pubDate>
            <atom:updated>2026-07-06T11:24:11.599Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Laravel code quality improvement with Larastan static analysis." src="https://cdn-images-1.medium.com/max/1024/1*1iDLWrvgBVlAxx2xoBl_eg.png" /><figcaption>Build cleaner Laravel applications with Larastan.</figcaption></figure><blockquote><em>“First, solve the problem. Then, write the code.”<br>— John Johnson</em></blockquote><h3>Key Takeaways</h3><ul><li>Larastan integrates PHPStan into Laravel for advanced static analysis.</li><li>Detects bugs and type errors without running your code.</li><li>Helps to improve maintainability, reliability, and developer confidence.</li><li>Supports Laravel 9 and above, with different Larastan versions.</li><li>Supports strict level-based analysis with customizable rules and baselines.</li></ul><h3>Index</h3><ol><li>Introduction</li><li>Supported Laravel Versions</li><li>Installing and Configuring Larastan</li><li>Larastan Levels Breakdown</li><li>Ignoring Errors</li><li>Baseline File</li><li>Laravel-Specific Rules</li><li>Custom types Doc</li><li>Use PHPStan plugin in PHPStorm</li><li>FAQs</li><li>Stats</li><li>Interesting Facts</li><li>Conclusion</li></ol><h3>1. Introduction</h3><p>Larastan is a PHPStan wrapper that brings powerful static analysis capabilities to Laravel projects. It finds bugs, type mismatches, and dead code in your application without executing a single line of it.</p><blockquote><em>“The best way to predict the future is to invent it.”— Alan Kay</em></blockquote><h3>2. Supported Laravel Versions</h3><p><a href="https://gist.githubusercontent.com/vatsalach-addweb/1da3ad34d234aec7070cc126c4aa95d5/raw/91fedfa35cda78e03569b408313c32952f311863/larastan_versions.md"><strong>gist.githubusercontent.com</strong></a></p><h3>3. Installing and Configuring Larastan</h3><p><strong>Installation</strong></p><p>To install Larastan, run the following command:</p><pre>composer require --dev &quot;larastan/larastan:^3.0&quot;</pre><p><strong>Configuration</strong></p><p>Create a <strong>phpstan.neon</strong> file in the root directory with the following configuration:</p><pre>includes:<br>     - vendor/larastan/larastan/extension.neon<br>parameters:<br>   paths:<br>        - app # You can add required folders here<br>   level: 5  # You can increase levels for more strict checking</pre><p><strong>Running Larastan</strong></p><p>To analyze your code, execute:</p><pre>./vendor/bin/phpstan analyse</pre><p>If you are getting the error <strong>Allowed memory size exhausted</strong>, then you can use the- <strong>memory-limit</strong> option fix the problem:</p><pre>./vendor/bin/phpstan analyse --memory-limit=2G</pre><p>For strictest level checking, use:</p><pre>./vendor/bin/phpstan analyse --level=9</pre><h3>4. Larastan Levels Breakdown</h3><p><a href="https://gist.githubusercontent.com/vatsalach-addweb/5c6e2b53b1617e4f81bbd5c07659f657/raw/32af37dedd9b24ac210d7c838755c72c9d04f850/larastan_error_levels.md"><strong>gist.githubusercontent.com</strong></a></p><blockquote><em>“Automation applied to an efficient operation will magnify the efficiency.”<br> — Bill Gates</em></blockquote><h3>5. Ignoring Errors</h3><p>When ignoring errors in PHPStan’s configuration file, they are ignored by writing a regex based on error messages:</p><pre>parameters:<br>    ignoreErrors:<br>        - &#39;#Call to an undefined method .*badMethod\(\)#&#39;</pre><h3>6. Baseline File</h3><p>In older codebases it might be hard to spend the time fixing all the code to pass a high PHPStan Level.</p><p>To get around this a baseline file can be generated. The baseline file will create a configuration file with all of the current errors, so new code can be written following a higher standard than the old code. (<a href="https://phpstan.org/user-guide/baseline">PHPStan Docs</a>)</p><pre>./vendor/bin/phpstan analyse --generate-baseline</pre><h3>7. Laravel-Specific Rules</h3><p>A list of configurable rules specific to Laravel can be found <a href="https://github.com/larastan/larastan/blob/3.x/docs/rules.md">here</a>.</p><h3>8. Custom types Doc</h3><p>A list of custom type doc is <a href="https://github.com/larastan/larastan/tree/3.x/docs">here</a>. Which includes <strong>custom-config-parameters.md , custom-types.md , errors-to-ignore.md</strong> etc. files.</p><h3>9. Use PHPStan plugin in PHPStorm</h3><p>Refer <a href="https://www.jetbrains.com/help/phpstorm/using-phpstan.html#enabling-tool-inspection">this</a> for enabling the phpstan in phpstorm without composer</p><h3>10. FAQs</h3><p><strong>Q: Can Larastan catch runtime exceptions?</strong><br>A: No, Larastan only performs static analysis. It won’t catch exceptions thrown at runtime, but it will highlight unsafe code patterns.</p><p><strong>Q: Is it safe to run Larastan on production code?</strong><br>A: Absolutely! Larastan doesn’t execute any code. It only analyzes it.</p><p><strong>Q: Should I aim for level 9 always?</strong><br>A: Ideally yes, but start small. Use baselines to incrementally improve quality.</p><p><strong>Q: Can I customize rules for my project?</strong><br>A: Yes! You can add custom rules, ignore specific errors, and tune paths.</p><h3>11. Stats</h3><ul><li>At the highest level (Level 9), Larastan and PHPStan together detect over 100 categories of potential issues. (<a href="https://phpstan.org/user-guide/rule-levels">PHPStan Rule Levels, Larastan README</a>)</li><li>Projects adopting Larastan often report significantly fewer runtime errors (anecdotally in the range of 25–50%) thanks to early detection of type and logic issues. (<a href="https://ma.ttias.be/phpstan-laravel-larastan-static-analysis-laravel-apps/">Mattias Geniar Blog Post, Larastan GitHub Discussions, Medium Article about PHPStan Adoption</a>)</li><li>Larastan has over 4 million downloads and is used in thousands of open-source Laravel repositories to improve code quality. (<a href="https://github.com/larastan/larastan">Larastan GitHub</a>)</li></ul><h3>12. Interesting Facts</h3><ul><li>Larastan was created by <a href="https://github.com/nunomaduro">Nuno Maduro</a>, the creator of PestPHP.</li><li>It internally uses <a href="https://phpstan.org/">PHPStan</a>, one of the most respected tools for static analysis in PHP.</li><li>You can extend Larastan using custom rules for your specific business logic!</li></ul><h3>13. Conclusion</h3><p>Larastan is more than a tool- it’s a code quality companion for Laravel developers. By integrating static analysis early in your development workflow, you prevent bugs, enforce best practices, and build confidence in your codebase.</p><ul><li>Start with level 5</li><li>Use baselines for legacy code</li><li>Gradually level up to 9</li><li>Fix errors as part of regular code reviews</li></ul><p>Use it. Learn from it. Make your Laravel code bulletproof.</p><h3>Hashtags</h3><p>Laravel #Larastan #PHP #PHPStan #WebDevelopment #SoftwareDevelopment #Programming #CodeQuality #StaticAnalysis #CleanCode #BackendDevelopment #LaravelDevelopment #PHPDeveloper #DeveloperTools #Coding #TechTips #BestPractices</p><p><strong>About the Author:</strong> <em>Mayank is a web developer at </em><a href="https://www.addwebsolution.com/our-capabilities/laravel-development-agency">AddWebSolution</a><em>, building scalable apps with PHP, Node.js &amp; React. Sharing ideas, code, and creativity.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c7d177160890" width="1" height="1" alt=""><hr><p><a href="https://medium.com/addweb-engineering/enhancing-laravel-code-quality-with-larastan-c7d177160890">Enhancing Laravel Code Quality with Larastan</a> was originally published in <a href="https://medium.com/addweb-engineering">AddWeb Digital</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Laravel Pivot Tables Explained: Clean Handling of Many-to-Many Relationships]]></title>
            <link>https://medium.com/addweb-engineering/laravel-pivot-tables-explained-clean-handling-of-many-to-many-relationships-71e33404b2d9?source=rss-491a93312824------2</link>
            <guid isPermaLink="false">https://medium.com/p/71e33404b2d9</guid>
            <category><![CDATA[laravel-developer]]></category>
            <category><![CDATA[coding-tips]]></category>
            <category><![CDATA[laravel-tutorials]]></category>
            <category><![CDATA[php-developers]]></category>
            <category><![CDATA[laravel]]></category>
            <dc:creator><![CDATA[Mayank-Addweb]]></dc:creator>
            <pubDate>Wed, 10 Dec 2025 06:49:16 GMT</pubDate>
            <atom:updated>2025-12-10T06:50:13.742Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Illustration showing two developers discussing Laravel code with the Laravel logo, representing clean handling of many-to-many relationships using pivot tables." src="https://cdn-images-1.medium.com/max/1024/1*NBLAXBMsNPfqxio_-_13Yw.png" /><figcaption><em>Handling Many-to-Many Relationships in Laravel</em></figcaption></figure><blockquote><em>“Bad programmers worry about the code. Good programmers worry about data structures and their relationships.”<br> — Linus Torvalds</em></blockquote><h3>Key Takeaways</h3><ul><li>Pivot tables manage many-to-many relationships in Laravel.</li><li>They don’t usually require their own Eloquent model.</li><li>Laravel simplifies pivot interaction with intuitive syntax.</li><li>You can add extra fields to a pivot for rich metadata (e.g., grades, timestamps).</li><li>Advanced use includes custom pivot models, filtering, and accessors.</li></ul><h3>Index</h3><ol><li>What is a Pivot Table?</li><li>When to Use Pivot Tables</li><li>Human-Friendly Analogy</li><li>Creating Pivot Tables in Laravel</li><li>Defining Relationships in Models</li><li>Interacting with Pivot Tables</li><li>Adding Extra Data to Pivot Tables</li><li>Advanced Pivot Table Usage</li><li>Behind the Scenes</li><li>Stats</li><li>Interesting Facts</li><li>FAQs</li><li>Conclusion</li></ol><h3>1. What is a Pivot Table?</h3><p>A <strong>pivot table</strong> is an intermediate table that connects two other tables in a many-to-many relationship. It doesn’t usually need its own Eloquent model but acts as a bridge storing relationship metadata.</p><h3>2. When to Use Pivot Tables</h3><p>You should use a pivot table when:</p><ul><li>One record in Table A can relate to many records in Table B</li><li>And one record in Table B can relate to many records in Table A</li></ul><p><strong>Examples:</strong></p><ul><li>Users and Roles</li><li>Students and Courses</li><li>Products and Categories</li></ul><blockquote><em>“Technology is best when it brings people together.”<br> — Matt Mullenweg</em></blockquote><h3>3. Human-Friendly Analogy</h3><p>Imagine you’re a student enrolling in university courses. You can take many courses, and each course has many students. The <strong>enrollment</strong> record linking them is the <strong>pivot table</strong> — it holds the connection, not the content.</p><blockquote><em>“Great things are done by a series of small things brought together.”<br> — Vincent Van Gogh</em></blockquote><h3>4. Creating Pivot Tables in Laravel</h3><p><strong>Step 1: Create the Main Tables</strong></p><pre>Schema::create(&#39;students&#39;, function (Blueprint $table) {<br>    $table-&gt;id();<br>    $table-&gt;string(&#39;name&#39;);<br>    $table-&gt;timestamps();<br>});</pre><pre>Schema::create(&#39;courses&#39;, function (Blueprint $table) {<br>    $table-&gt;id();<br>    $table-&gt;string(&#39;title&#39;);<br>    $table-&gt;timestamps();<br>});</pre><p><strong>Step 2: Create the Pivot Table</strong></p><pre>Schema::create(&#39;course_student&#39;, function (Blueprint $table) {<br>    $table-&gt;id();<br>    $table-&gt;foreignId(&#39;student_id&#39;)-&gt;constrained()-&gt;onDelete(&#39;cascade&#39;);<br>    $table-&gt;foreignId(&#39;course_id&#39;)-&gt;constrained()-&gt;onDelete(&#39;cascade&#39;);<br>    $table-&gt;timestamps();<br>});</pre><p><strong>Note:</strong> Laravel expects the pivot table name to be in alphabetical order: <strong>course_student</strong>, not <strong>student_course</strong>.</p><h3>5. Defining Relationships in Models</h3><p><strong>Student.php</strong></p><pre>public function courses()<br>{<br>    return $this-&gt;belongsToMany(Course::class);<br>}</pre><p><strong>Course.php</strong></p><pre>public function students()<br>{<br>    return $this-&gt;belongsToMany(Student::class);<br>}</pre><h3>6. Interacting with Pivot Tables</h3><p><strong>Attaching Records</strong></p><pre>$student-&gt;courses()-&gt;attach([1, 2]);</pre><p><strong>Detaching Records</strong></p><pre>$student-&gt;courses()-&gt;detach(2);</pre><p><strong>Syncing Records</strong></p><pre>$student-&gt;courses()-&gt;sync([2, 3]);</pre><h3>7. Adding Extra Data to Pivot Tables</h3><p><strong>Migration Example</strong></p><pre>Schema::create(&#39;course_student&#39;, function (Blueprint $table) {<br>    $table-&gt;id();<br>    $table-&gt;foreignId(&#39;student_id&#39;)-&gt;constrained()-&gt;onDelete(&#39;cascade&#39;);<br>    $table-&gt;foreignId(&#39;course_id&#39;)-&gt;constrained()-&gt;onDelete(&#39;cascade&#39;);<br>    $table-&gt;string(&#39;grade&#39;)-&gt;nullable();<br>    $table-&gt;timestamp(&#39;enrolled_at&#39;)-&gt;nullable();<br>    $table-&gt;timestamps();<br>});</pre><p><strong>Attaching with Additional Data</strong></p><pre>$student-&gt;courses()-&gt;attach(1, [<br>    &#39;grade&#39; =&gt; &#39;A&#39;,<br>    &#39;enrolled_at&#39; =&gt; now(),<br>]);</pre><p><strong>Accessing Pivot Data</strong></p><pre>foreach ($student-&gt;courses as $course) {<br>    echo $course-&gt;pivot-&gt;grade;<br>    echo $course-&gt;pivot-&gt;enrolled_at;<br>}</pre><h3>8. Advanced Pivot Table Usage</h3><p><strong>Custom Pivot Models</strong></p><p>If you want to handle logic on the pivot itself:</p><pre>class Enrollment extends Pivot<br>{<br>    use SoftDeletes;<br>}</pre><p><strong>In Student.php:</strong></p><pre>return $this-&gt;belongsToMany(Course::class)<br>            -&gt;using(Enrollment::class)<br>            -&gt;withTimestamps();</pre><p><strong>Filtering by Pivot Fields</strong></p><pre>$student-&gt;courses()-&gt;wherePivot(&#39;grade&#39;, &#39;A&#39;)-&gt;get();</pre><p><strong>Updating Pivot Table Values</strong></p><pre>$student-&gt;courses()-&gt;updateExistingPivot($courseId, [<br>    &#39;grade&#39; =&gt; &#39;B&#39;<br>]);</pre><p><strong>Syncing with Data</strong></p><pre>$student-&gt;courses()-&gt;sync([<br>    1 =&gt; [&#39;grade&#39; =&gt; &#39;A&#39;],<br>    2 =&gt; [&#39;grade&#39; =&gt; &#39;B&#39;]<br>]);</pre><p><strong>Using Pivot Accessors</strong></p><p>Add accessors to format pivot data:</p><pre>public function getFormattedGradeAttribute()<br>{<br>    return strtoupper($this-&gt;pivot-&gt;grade);<br>}</pre><h3>9. Behind the Scenes</h3><p>Laravel uses the <strong>BelongsToMany</strong> relationship internally to manage pivot tables. All pivot methods — attach, detach, sync — are part of this relationship class.</p><p>For advanced use cases, you can define a custom Pivot model by extending <strong>Illuminate\Database\Eloquent\Relations\Pivot.</strong></p><blockquote><em>“Be stubborn on vision but flexible on details.”<br> — Jeff Bezos</em></blockquote><h3>10. Stats</h3><ul><li>Many-to-many relationships (using pivot tables) are extremely common in real-world Laravel applications</li><li>Pivot tables can store extra metadata fields (like status, timestamps, or custom attributes) without significant performance overhead. (<a href="https://laravel.com/docs/12.x/eloquent-relationships#defining-custom-intermediate-table-models">Laravel Docs — Defining Custom Pivot Models</a>)</li><li>By default, pivot data is lazy-loaded; you must use withPivot() to retrieve additional pivot columns during eager loading. (<a href="https://laravel.com/docs/12.x/eloquent-relationships#retrieving-intermediate-table-columns">Laravel Docs Retrieving Intermediate Table Columns</a>)</li></ul><h3>11. Interesting Facts</h3><ul><li>Laravel automatically updates pivot timestamps if -&gt;withTimeStamps() is used. (<a href="https://laravel.com/docs/12.x/eloquent-relationships#updating-a-record-on-the-intermediate-table">Laravel Docs — Updating Pivot Tables</a>)</li><li>Laravel supports polymorphic many-to-many relationships, allowing a single pivot table to relate multiple model types. (<a href="https://laravel.com/docs/12.x/eloquent-relationships#many-to-many-polymorphic-relations">Laravel Docs — Many-to-Many Polymorphic Relationships</a>)</li><li>You can filter queries using pivot values with wherePivot()and havingPivot(). (<a href="https://laravel.com/docs/12.x/eloquent-relationships#filtering-queries-via-pivot-table-columns">Laravel Docs — Filtering by Pivot Columns</a>)</li><li>Use withPivot() to eager-load pivot data and access it like a property. (<a href="https://laravel.com/docs/12.x/eloquent-relationships#retrieving-intermediate-table-columns">Laravel Docs — Retrieving Intermediate Table Columns</a>)</li></ul><h3>12. FAQs</h3><p><strong>Q: Do pivot tables always need an Eloquent model?</strong><br>A: No, only when you need custom logic or features like soft deletes.</p><p><strong>Q: Can I store additional data like grades or timestamps?</strong><br>A: Yes! Add custom columns in the pivot migration and interact using -<strong>&gt;pivot-&gt;column</strong>.</p><p><strong>Q: Can pivot tables work with polymorphic relationships?</strong><br>A: Absolutely! Laravel supports <strong>morphToMany</strong> and <strong>morphedByMany</strong>.</p><p><strong>Q: Is there a naming convention?</strong><br>A: Yes, pivot table names should be in alphabetical order (e.g., <strong>course_student</strong>).</p><h3>13. Conclusion</h3><p>Pivot tables are essential for managing many-to-many relationships in Laravel. Whether you’re building a student enrollment system or managing user roles, they offer a powerful and clean way to store relational data. Laravel’s Eloquent makes it easy to define, interact with, and customize pivot tables — ensuring your relationships are efficient and expressive. With just a few lines of code, you unlock powerful relationship management features ready for real-world complexity.</p><p>About the Author: <em>Mayank is a web developer at </em><a href="https://www.addwebsolution.com/our-capabilities/laravel-development-agency"><em>AddWebSolution</em></a><em>, building scalable apps with PHP, Node.js &amp; React. Sharing ideas, code, and creativity.</em></p><p>#Laravel #LaravelDevelopers #LaravelTips #LaravelEloquent #LaravelPivot #ManyToMany #WebDevelopment #PHPDevelopers #BackendDevelopment #CodingTips #LaravelRelationships #EloquentRelationships #LaravelTutorial #LearnLaravel #FullStackDeveloper #WebDevCommunity #ProgrammingTips #TechBlogging #DevelopersLife</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=71e33404b2d9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/addweb-engineering/laravel-pivot-tables-explained-clean-handling-of-many-to-many-relationships-71e33404b2d9">Laravel Pivot Tables Explained: Clean Handling of Many-to-Many Relationships</a> was originally published in <a href="https://medium.com/addweb-engineering">AddWeb Digital</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Laravel Seeders & Factories- How to Generate Fake Data for Development]]></title>
            <link>https://medium.com/addweb-engineering/laravel-seeders-factories-how-to-generate-fake-data-for-development-e2a0062727d7?source=rss-491a93312824------2</link>
            <guid isPermaLink="false">https://medium.com/p/e2a0062727d7</guid>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[programming-guide]]></category>
            <category><![CDATA[database-seeding]]></category>
            <category><![CDATA[model-factory]]></category>
            <category><![CDATA[php-tips]]></category>
            <dc:creator><![CDATA[Mayank-Addweb]]></dc:creator>
            <pubDate>Wed, 26 Nov 2025 08:00:57 GMT</pubDate>
            <atom:updated>2025-11-26T11:36:33.250Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Illustration of a developer generating automated data using Laravel seeders and factories." src="https://cdn-images-1.medium.com/max/1024/1*3YYl5zMo36WhmKiGQr-2rA.png" /><figcaption>Automated data generation using Laravel seeders and factories.</figcaption></figure><blockquote><em>“The expert in anything was once a beginner.”<br> — Helen Hayes</em></blockquote><h3>xKey Takeaways</h3><p>Understand the difference between Seeders and Factories in Laravel.<br>Learn when and how to use create() vs make().<br>Use FakerPHP methods effectively for fake data.<br>Apply traditional and modern methods to create seeders and factories.<br>Use artisan commands efficiently during development.</p><p><strong>Index</strong></p><ol><li>Introduction</li><li>When to Use Seeder and Factory</li><li>Traditional vs Modern Usage</li><li>Faker Cheat Sheet — All Available Methods</li><li>Creating Factories</li><li>Creating Seeders</li><li>Handling Relationships</li><li>Real-World Examples</li><li>Advanced Tips</li><li>Testing with Factories</li><li>Difference Between create() and make()</li><li>Summary</li><li>Resources</li><li>Stats</li><li>Interesting Facts</li><li>FAQ’s</li><li>Conclusion</li></ol><h3>1. Introduction</h3><p>Seeders and Factories are essential tools in Laravel for generating fake data. They help speed up development and testing by providing sample data to work with. Laravel uses FakerPHP to generate fake information for factories.</p><p><strong>Factory:</strong> Blueprint to create fake model data.</p><p><strong>Seeder:</strong> Class used to insert data into the database.</p><h3>2. When to Use Seeder and Factory</h3><p>ScenarioUse Factory?Use Seeder?Automated TestingYesOptionalPopulating Dev DatabaseYesYesComplex Model RelationshipsYesYesStatic Data (e.g. Countries)NoYes</p><p><a href="https://gist.github.com/vatsalach-addweb/929d106a00c146281c2c27dd548104b9/raw/4ad9df3e1f6ed8670f45ef74678dbc118563e581/use_of_factory_and_seeder.md">view raw</a><a href="https://gist.github.com/vatsalach-addweb/929d106a00c146281c2c27dd548104b9#file-use_of_factory_and_seeder-md">use_of_factory_and_seeder.md </a>hosted with ❤ by <a href="https://github.com/">GitHub</a></p><p><strong>Examples:</strong></p><ul><li>Use factories in tests to create isolated data.</li><li>Use seeders for populating a realistic dataset in local or staging environments.</li></ul><h3>3. Traditional vs Modern Usage</h3><p>Before Laravel 8, generating fake data involved a more procedural approach. With Laravel 8+, the system evolved to become more structured, readable, and object-oriented. Let’s look at how factories and seeders have changed over time.</p><p><strong>Traditional (Pre-Laravel 8)</strong></p><pre>factory(User::class, 10)-&gt;create();</pre><p>This method relied on global helper functions and inline logic. It worked, but wasn’t as elegant or flexible.</p><p><strong>Modern (Laravel 8+)</strong></p><pre>php artisan make:factory UserFactory --model=User<br>php artisan make:seeder UserSeeder<br>User::factory()-&gt;count(10)-&gt;create();</pre><p>Laravel now uses class-based factories and seeders, making code more readable, testable, and maintainable.</p><h3>4. Faker Cheat Sheet — All Available Methods</h3><p>FakerPHP is like a magician for developers — it helps you generate realistic names, addresses, dates, text, and more. Whether you’re seeding a development database or writing automated tests, this cheat sheet gives you all the tools to fake it like a pro.</p><p><strong>Personal Info</strong></p><pre>$this-&gt;faker-&gt;name()<br>$this-&gt;faker-&gt;firstName()<br>$this-&gt;faker-&gt;lastName()<br>$this-&gt;faker-&gt;title()<br>$this-&gt;faker-&gt;email()<br>$this-&gt;faker-&gt;unique()-&gt;safeEmail()<br>$this-&gt;faker-&gt;username()<br>$this-&gt;faker-&gt;password()<br>$this-&gt;faker-&gt;phoneNumber()<br>$this-&gt;faker-&gt;address()</pre><p><strong>Dates &amp; Times</strong></p><pre>$this-&gt;faker-&gt;date()<br>$this-&gt;faker-&gt;dateTime()<br>$this-&gt;faker-&gt;dateTimeBetween(&#39;-1 week&#39;, &#39;+1 week&#39;)<br>$this-&gt;faker-&gt;time()</pre><p><strong>Payments</strong></p><pre>$this-&gt;faker-&gt;creditCardNumber()<br>$this-&gt;faker-&gt;creditCardType()<br>$this-&gt;faker-&gt;currencyCode()<br>$this-&gt;faker-&gt;randomFloat(2, 0, 9999)</pre><p><strong>Location</strong></p><pre>$this-&gt;faker-&gt;city()<br>$this-&gt;faker-&gt;state()<br>$this-&gt;faker-&gt;country()<br>$this-&gt;faker-&gt;postcode()<br>$this-&gt;faker-&gt;latitude()<br>$this-&gt;faker-&gt;longitude()</pre><p><strong>Text</strong></p><pre>$this-&gt;faker-&gt;word()<br>$this-&gt;faker-&gt;words(3, true)<br>$this-&gt;faker-&gt;sentence()<br>$this-&gt;faker-&gt;paragraph()<br>$this-&gt;faker-&gt;text(200)</pre><p><strong>Images &amp; Files</strong></p><pre>$this-&gt;faker-&gt;imageUrl(640, 480)<br>$this-&gt;faker-&gt;image(&#39;public/storage/images&#39;, 400, 300, null, false)</pre><p><strong>Numbers</strong></p><pre>$this-&gt;faker-&gt;randomDigit()<br>$this-&gt;faker-&gt;randomNumber()<br>$this-&gt;faker-&gt;numberBetween(1, 100)<br>$this-&gt;faker-&gt;randomFloat(2, 1, 1000)</pre><p><strong>Miscellaneous</strong></p><pre>$this-&gt;faker-&gt;uuid()<br>$this-&gt;faker-&gt;slug()<br>$this-&gt;faker-&gt;boolean()<br>$this-&gt;faker-&gt;regexify(&#39;[A-Za-z0-9]{10}&#39;)<br>$this-&gt;faker-&gt;url()<br>$this-&gt;faker-&gt;domainName()<br>$this-&gt;faker-&gt;company()<br>$this-&gt;faker-&gt;jobTitle()</pre><h3>5. Creating Factories</h3><pre>php artisan make:factory ProductFactory --model=Product</pre><p><strong>Sample Factory:</strong></p><pre>public function definition(): array<br>{<br>    return [<br>        &#39;name&#39; =&gt; $this-&gt;faker-&gt;word(),<br>        &#39;price&#39; =&gt; $this-&gt;faker-&gt;randomFloat(2, 1, 1000),<br>        &#39;description&#39; =&gt; $this-&gt;faker-&gt;sentence(),<br>    ];<br>}</pre><h3>6. Creating Seeders</h3><pre>php artisan make:seeder ProductSeeder</pre><p><strong>Sample Seeder:</strong></p><pre>public function run(): void<br>{<br>    Product::factory()-&gt;count(50)-&gt;create();<br>}</pre><p><strong>Call Seeder in DatabaseSeeder.php:</strong></p><pre>$this-&gt;call(ProductSeeder::class);</pre><h3>7. Handling Relationships</h3><p><strong>One-to-One</strong></p><pre>Profile::factory()-&gt;for(User::factory())-&gt;create();</pre><p><strong>One-to-Many</strong></p><pre>User::factory()-&gt;hasPosts(5)-&gt;create();</pre><p><strong>Many-to-Many</strong></p><pre>Post::factory()-&gt;hasTags(3)-&gt;create();</pre><h3>8. Real-World Examples</h3><p><strong>Blog App:</strong></p><ul><li>Users → hasMany → Posts</li><li>Posts → hasMany → Comments</li></ul><p><strong>Step-by-Step — Models and Relationships:</strong></p><pre>// User.php<br>public function posts() {<br>    return $this-&gt;hasMany(Post::class);<br>}</pre><pre>// Post.php<br>public function user() {<br>    return $this-&gt;belongsTo(User::class);<br>}<br>public function comments() {<br>    return $this-&gt;hasMany(Comment::class);<br>}</pre><pre>// Comment.php<br>public function post() {<br>    return $this-&gt;belongsTo(Post::class);<br>}</pre><p><strong>Factory Definitions</strong></p><pre>// database/factories/UserFactory.php<br>public function definition(): array {<br>    return [<br>        &#39;name&#39; =&gt; fake()-&gt;name(),<br>        &#39;email&#39; =&gt; fake()-&gt;unique()-&gt;safeEmail(),<br>        &#39;password&#39; =&gt; bcrypt(&#39;password&#39;),<br>    ];<br>}</pre><pre>// database/factories/PostFactory.php<br>public function definition(): array {<br>    return [<br>        &#39;title&#39; =&gt; fake()-&gt;sentence(),<br>        &#39;body&#39; =&gt; fake()-&gt;paragraph(5),<br>    ];<br>}</pre><pre>// database/factories/CommentFactory.php<br>public function definition(): array {<br>    return [<br>        &#39;content&#39; =&gt; fake()-&gt;sentence(),<br>    ];<br>}</pre><p><strong>Seeder Example</strong></p><pre>// DatabaseSeeder.php<br>public function run(): void<br>{<br>    \App\Models\User::factory()<br>        -&gt;count(10)<br>        -&gt;has(<br>            \App\Models\Post::factory()<br>                -&gt;count(5)<br>                -&gt;has(\App\Models\Comment::factory()-&gt;count(3))<br>        )<br>        -&gt;create();<br>}</pre><p><strong>E-Commerce:</strong></p><ul><li>Product → belongsTo → Category</li><li>Order → hasMany → OrderItems</li><li>OrderItems -&gt; belongsTo -&gt;Product</li></ul><p><strong>Step-by-Step — Models and Relationships:</strong></p><pre>// Category.php<br>public function products() {<br>    return $this-&gt;hasMany(Product::class);<br>}</pre><pre>// Product.php<br>public function category() {<br>    return $this-&gt;belongsTo(Category::class);<br>}</pre><pre>// Order.php<br>public function items() {<br>    return $this-&gt;hasMany(OrderItem::class);<br>}</pre><pre>// OrderItem.php<br>public function order() {<br>    return $this-&gt;belongsTo(Order::class);<br>}<br>public function product() {<br>    return $this-&gt;belongsTo(Product::class);<br>}</pre><p><strong>Factory Definitions</strong></p><pre>// CategoryFactory.php<br>public function definition(): array {<br>    return [<br>        &#39;name&#39; =&gt; fake()-&gt;word(),<br>    ];<br>}</pre><pre>// ProductFactory.php<br>public function definition(): array {<br>    return [<br>        &#39;name&#39; =&gt; fake()-&gt;word(),<br>        &#39;price&#39; =&gt; fake()-&gt;randomFloat(2, 10, 1000),<br>        &#39;category_id&#39; =&gt; Category::factory(),<br>    ];<br>}</pre><pre>// OrderFactory.php<br>public function definition(): array {<br>    return [<br>        &#39;user_id&#39; =&gt; User::factory(),<br>        &#39;status&#39; =&gt; fake()-&gt;randomElement([&#39;pending&#39;, &#39;shipped&#39;, &#39;delivered&#39;]),<br>    ];<br>}</pre><pre>// OrderItemFactory.php<br>public function definition(): array {<br>    return [<br>        &#39;product_id&#39; =&gt; Product::factory(),<br>        &#39;quantity&#39; =&gt; fake()-&gt;numberBetween(1, 5),<br>    ];<br>}</pre><p><strong>Seeder Example</strong></p><pre>public function run(): void<br>{<br>    // Categories and Products<br>    \App\Models\Category::factory()<br>        -&gt;count(5)<br>        -&gt;has(\App\Models\Product::factory()-&gt;count(10))<br>        -&gt;create();</pre><pre>    // Orders and Items<br>    \App\Models\Order::factory()<br>        -&gt;count(20)<br>        -&gt;has(<br>            \App\Models\OrderItem::factory()<br>                -&gt;count(3)<br>        )<br>        -&gt;create();<br>}</pre><p><strong>Command to Run Seeder:</strong></p><pre>php artisan migrate:fresh --seed</pre><h3>9. Advanced Tips</h3><p><strong>Seed Only If Empty</strong></p><pre>if (User::count() == 0) {<br>    User::factory(10)-&gt;create();<br>}</pre><p><strong>Avoid Production Seeding</strong></p><pre>if (!app()-&gt;isProduction()) {<br>    $this-&gt;call(DevSeeder::class);<br>}</pre><p><strong>Migrate &amp; Seed in One Step</strong></p><pre>php artisan migrate:fresh --seed</pre><h3>10. Testing with Factories</h3><pre>public function test_post_creation()<br>{<br>    $user = User::factory()-&gt;create();<br>    $post = Post::factory()-&gt;for($user)-&gt;create();</pre><pre>    $this-&gt;assertDatabaseHas(&#39;posts&#39;, [&#39;user_id&#39; =&gt; $user-&gt;id]);<br>}</pre><h3>11. Difference Between create() and make()</h3><p>MethodPersists to DB?Returns Model?Typical Usecreate()YesYesSeeding, testing with DB checkmake()NoYesUnit tests or manipulation before saving</p><p><a href="https://gist.github.com/vatsalach-addweb/81b1b7d23d99413ab5b09f0500474169/raw/700110dab53a090c77bd77b4db05687e34295d8b/diff_between_create_and_make.md">view raw</a><a href="https://gist.github.com/vatsalach-addweb/81b1b7d23d99413ab5b09f0500474169#file-diff_between_create_and_make-md">diff_between_create_and_make.md </a>hosted with ❤ by <a href="https://github.com/">GitHub</a></p><p><strong>create() Example:</strong></p><pre>$user = User::factory()-&gt;create();</pre><p>This creates a user and inserts into the database.</p><p><strong>make() Example:</strong></p><pre>$user = User::factory()-&gt;make();</pre><p>This creates a user but doesn’t insert into the database. Useful for testing model logic without hitting the DB.</p><p><strong>Human-friendly analogy:</strong><br>Think of <strong>make()</strong> as drawing a character on paper (you haven’t created it in real life yet).<br><strong>create()</strong> is like actually 3D printing that character into existence (database).</p><blockquote><em>“Dream big. Start small. Act now.”<br> — Robin Sharma</em></blockquote><h3>12. Summary</h3><p>Factory = Recipe (How to create data)</p><ul><li>Seeder = Chef (Creates and inserts the data)</li><li>Faker = Ingredients (Fake fields)</li><li>make() = Draft copy, not saved</li><li>create() = Final copy, saved to DB</li></ul><p>Use factories for fast, repeatable data creation, especially for tests. Use seeders to load your database with dev data or static reference data.</p><blockquote><em>“Learning never exhausts the mind.”<br> — Leonardo da Vinci</em></blockquote><h3>13. Resources</h3><ul><li>Laravel Docs: <a href="https://laravel.com/docs/12.x/seeding">https://laravel.com/docs/12.x/seeding</a></li><li>FakerPHP Docs: <a href="https://fakerphp.org/">https://fakerphp.org/</a></li><li>GitHub Laravel Examples: <a href="https://github.com/laravel/laravel/tree/master/database/factories">https://github.com/laravel/laravel/tree/master/database/factories</a></li></ul><h3>14. Stats</h3><ul><li>Laravel 12 supports class-based seeders and factories by default.<a href="https://laravel.com/docs/12.x/seeding">(Laravel 12 Documentation — Database: Seeding, Laravel 12 Documentation — Database: Factories)</a></li><li>FakerPHP has over 50+ methods for generating fake data. <a href="https://fakerphp.org/formatters/">(FakerPHP Official Documentation — Formatters)</a></li><li>Factory usage in Laravel projects increased by 40% since Laravel 8’s class-based system.</li></ul><h3>15. Interesting Facts</h3><ul><li>The Faker library was originally a Ruby gem before being ported to PHP. <a href="https://fakerphp.org/">(Faker Ruby Gem, FakerPHP History)</a></li><li>Laravel 12 allows factory()-&gt;has() relationships that simulate real-world relational data effortlessly. <a href="https://laravel.com/docs/12.x/database-testing#has-relationships">(Laravel Factories — Has Relationships)</a></li><li>Faker can be customized by locale — meaning you can generate region-specific names, addresses, etc. <a href="https://fakerphp.org/formatters/#localization">(FakerPHP Locales)</a></li></ul><h3>16. FAQ’s</h3><p><strong>Q: Can I use factories without seeders?</strong><br>Yes! Especially useful in unit testing or test classes.</p><p><strong>Q: Can I run factories in production?</strong><br>Not recommended. They are intended for testing/dev environments.</p><p><strong>Q: How do I use Faker with a specific locale?</strong><br>Pass locale to the factory class or override $faker with a new instance.</p><p><strong>Q: What if I want unique data each time?</strong><br>Use methods like $this-&gt;faker-&gt;unique()-&gt;email() to avoid duplicates.</p><p><strong>Q: How can I define custom states?</strong><br>Use the state() method in factories to define preset variations.</p><h3>17. Conclusion</h3><p>Laravel Seeders and Factories are indispensable tools for every developer. They ensure smooth development, easy testing, and robust prototyping. By mastering make() vs create(), utilizing Faker’s wide range of data generators, and leveraging artisan commands, you can simulate any data-driven scenario. Whether you’re building a blog, marketplace, or SaaS — this is your cheat code for rapid development.</p><p>About the Author: <em>Mayank is a web developer at </em><a href="https://www.addwebsolution.com/"><em>AddWebSolution</em></a><em>, building scalable apps with PHP, Node.js &amp; React. Sharing ideas, code, and creativity.</em></p><p>#Laravel #Laravel12 #PHP #LaravelDevelopers #LaravelSeeders #LaravelFactories #FakerPHP #WebDevelopment #BackendDevelopment #PHPTutorial #LaravelTips #LaravelCommunity #CodingTips #WebDevCommunity #DatabaseSeeding #SoftwareDevelopment #DevTips #ProgrammingGuide #LaravelEcosystem</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e2a0062727d7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/addweb-engineering/laravel-seeders-factories-how-to-generate-fake-data-for-development-e2a0062727d7">Laravel Seeders &amp; Factories- How to Generate Fake Data for Development</a> was originally published in <a href="https://medium.com/addweb-engineering">AddWeb Digital</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>