Get "PHP 8 in a Nutshell" (Soon PHP 8.5)
Amit Merchant
Amit Merchant

A blog on PHP, JavaScript, and more

How to use Laravel 7.x model factories in Laravel 8

Laravel 8’s class-based model factories are great and I’ve written an entire article about what they are and how they are more feature-rich and useful than the traditional factories that we used to write prior to Laravel 8.

So, if you’re spinning up a new application from Laravel 8, you’ll be obviously using class-based factories but if you’re upgrading to Laravel 8, from let’s say Laravel 7.x, you might be considering using the old factories for a number of reasons. One of being your application already has a lot of factories written and you don’t want to refactor them to the class-based ones all at once.

The laravel/legacy-factories package

In scenarios like this, you can install the laravel/legacy-factories composer package into your application which will allow you to use the legacy/traditional factories in your Laravel application without changing anything in your application.

You can install this package using Composer like so.

$ composer require laravel/legacy-factories

This is a first-party package developed by the Laravel team itself. So, no issues of authenticity over there.

In closing

Although you can continue using the legacy factories using this package, I would highly recommend using the newest class-based factories as those provide more features and are easy to manage as well. So, you should consider refactoring your existing ones slowly over time, in my opinion.

Coming Soon: PHP 8.5
Learn the fundamentals of PHP 8 (including 8.1, 8.2, 8.3, and 8.4), the latest version of PHP, and how to use it today with my book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

👋 Hi there! This is Amit, again. I write articles about all things web development. If you enjoy my work (the articles, the open-source projects, my general demeanour... anything really), consider leaving a tip & supporting the site. Your support is incredibly appreciated!

Comments?