nWidart / laravel-modules

Module Management In Laravel
https://docs.laravelmodules.com
MIT License
5.52k stars 959 forks source link

Target class [Illuminate\Database\Eloquent\Factory] does not exist. #1339

Closed arkniazi closed 2 years ago

arkniazi commented 2 years ago

Getting error after upgrading laravel framework from 7 to 8.

Illuminate\Contracts\Container\BindingResolutionException

Target class [Illuminate\Database\Eloquent\Factory] does not exist.

Error is in ServiceProvider of the module. It can't locate factory call.

dcblogdev commented 2 years ago

This is due to how factories were changed into classes in Laravel 8. You either need to remove the factory reference from your service providers or install Laralvel's legacy factory package to support old style factories.

composer require laravel/legacy-factories
arkniazi commented 2 years ago

Yes I have removed that code. Because right now I don't have any factories in the module. And also I'm moving all the factories classes to newer version.

But if I add the factories using newer version in Modules. Will they automatically loaded? Now that I have removed the reference.

dcblogdev commented 2 years ago

yes, they are loaded by their namespaces so you only need to reference the factories from the models

For example:

class Contact extends Model
{
    use HasFactory;

    protected $fillable = ['name', 'email'];

    protected static function newFactory()
    {
        return ContactFactory::new();
    }
}

I cover using Factories with modules in my Laravel Modules book https://dcblogdev.gumroad.com/l/laravel-the-modular-way

CleanShot 2022-01-06 at 09 08 13@2x
arkniazi commented 2 years ago

Thanks a lot @dcblogdev for the details response.