Get "PHP 8 in a Nuthshell" (Soon includes PHP 8.4)
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.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new 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.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?