Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Feat: factory generic in make:model command #52855

Merged
merged 6 commits into from
Oct 1, 2024

Conversation

MrPunyapal
Copy link
Contributor

Based on this comment by @taylorotwell, I have adjusted the make:model command to include a factory doc-block.

For example, if I run the following command:

php artisan make:model Post

I will get a file with the code below, as it was before:

<?php

namespace App\Models;

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

class Post extends Model
{
    use HasFactory;
}

However, if I run the following command:

php artisan make:model Comment -f

or any other command that generates a factory along with it, the resulting file will contain the code below:

<?php

namespace App\Models;

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

class Comment extends Model
{
    /** @use HasFactory<\Database\Factories\CommentFactory> */
    use HasFactory;
}

@MrPunyapal MrPunyapal changed the title Feat: factory generic in model [11.x] Feat: factory generic in model Sep 19, 2024
@Jubeki
Copy link
Contributor

Jubeki commented Sep 20, 2024

Just as an idea: Could you not also replace the HasFactory use with an empty string if the flag --factory is not set?

(Change in behaviour, but acceptable in my opinion)

@MrPunyapal
Copy link
Contributor Author

@Jubeki
I thought for the same

But yeah it's a change in behaviour!

I would like to see @taylorotwell's thoughts on the same.

@MrPunyapal MrPunyapal changed the title [11.x] Feat: factory generic in model [11.x] Feat: factory generic in make:model command Sep 29, 2024
@taylorotwell taylorotwell merged commit 22bc32f into laravel:11.x Oct 1, 2024
31 checks passed
@MrPunyapal MrPunyapal deleted the feat/factory-generic-in-model branch October 1, 2024 14:28
@jasonmccreary
Copy link
Contributor

@MrPunyapal, something seems off here... The factory option existed, but now it adds a DocBlock. However, from what I read, disabling the factory option doesn't add the DocBlock or create the factory, but the import still remains.

I appreciate some of this may predate this PR. But seems one of these paths may be buggy.

@MrPunyapal
Copy link
Contributor Author

@jasonmccreary
Yes if we want to make it work with use Hasfactory then we also need to take care of an import statement too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants