Category: Laravel

Refactoring to Laravel 8 Class Model Factories | C.S. Rhymes C.S. Rhymes HomeBooksNigel's Intranet AdventureHow NOT to make a WebsiteHow NOT to use a SmartphoneMy WorkThemesPackagesSitesDemosStoriesSponsorsAboutBlogSponsorRefactoring to Laravel 8 Class Model FactoriesPublished: Jul 20, 2021 by C.S. RhymesLaravel 8 introduced new class based model factories and if you have an existing project you can use the legacy factories package to keep using the old factories. I have tended to keep the factories as they were and continue development, but after working on a fresh Laravel 8 project and using the new class based syntax I decided to go back and update the factories in the older Laravel apps. This article explains how I went about refactoring the factories to classes.In this scenario we have a Laravel app that allows you to create a post. The post has a title, content, author and is either published or not published.Original FactoryHere is the PostFactory in the previous format.

It has a state called published where the publish value is set to true.// database/factories/PostFactory.php
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners