Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

trevorpan's avatar

add HasFactory trait to Model::class() vs. each model?

If each model extends:

<?php

namespace App\Models;

...

class Order extends Model
{

Is it reasonable to just add it to the Model class?

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model as Eloquent;

class Model extends Eloquent
{
    use HasFactory;
    protected $guarded = [];
}
0 likes
4 replies
MichalOravec's avatar

It doesn't matter. When you make a new model. You have two options: add HasFactory trait or change extended class. In both of them you have to change model.

So in my opinion it's better to just add HasFactory trait.

trevorpan's avatar

On each new model?

I'm still trying to sort out, as a practice, when extra work is worth it - to make the code more clear. Or if it adds to much overhead...

MichalOravec's avatar
Level 75

On each new model where you want to add that trait.

trevorpan's avatar

Hi @michaloravec appreciate your tip. Going through the l8 updates. Definitely pulled in composer require laravel/legacy-factories seems like a beast to update a big test suite!

Please or to participate in this conversation.