Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method 'loadFactoriesFrom' is deprecated

Tags:

php

laravel

In have recently upgraded to Laravel 8 but now I am receiving a warning

Method 'loadFactoriesFrom' is deprecated

My file structure is like below

myapp/
├── packages/
│   ├── package-1/
|   |   |── src/
|   |   |   |── databases/
|   |   |   |   |── factories
|   |   |   |   |   |── PackageModel11
|   |   |   |   |   |── PackageModel12
|   |   |   |   |── Package1ServiceProvider
|   |   |   |   └── migrations
|   |   |   |── models/
|   |   |   |── controllers/
|   |   |   |── migrations/
|   |   |   └── /
|   |   |── tests/
|   |   |── composer.json/
│   ├── app
│   ├── database/
│   ├── resources/
│   └── public/

To Load my custom package I use various functions

<?php

namespace MyName\Package1;

use Illuminate\Support\ServiceProvider;

class Package1ServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->loadFactoriesFrom(['database/factories']);
        $this->loadRoutesFrom(__DIR__.'/routes.php');
        $this->loadMigrationsFrom(__DIR__.'/migrations');
        $this->loadViewsFrom(__DIR__.'/views', '');
        $this->publishes([
            __DIR__.'/views' => base_path('resources/views'),
        ]);
    }

    public function register()
    {
        // Register controllers Here 
    }
}

How do I load these factories now that loadFactoriesFrom is deprecated?

Update

I found here how I can connect models and Factories and they document to override the newFactory() function.

So I have below in my Test

  public function testWithPermissionUserCanSaveNewOnlineAssessment()
  {
    $topic = ELearningTopic::factory()->create();
    $this->actingAs($this->user, 'api')
      ->post('api/e-learning/course-content/topics/.' . $topic->id . './online-assessments', [])
      ->assertStatus(401);
  }

My Factory

class ELearningTopicFactory extends Factory
{
  public function definition()
  {
     return [
       'name' => 'Some name'
     ];
  }
}

And my Model

  use HasFactory;
  protected static function newFactory()
  {
    return ELearningTopicFactory::new();
  }

Now I am getting Error Below

Class 'App\ELearningTopic' not found

What could I be missing? why isnt the model class overridden

like image 984
Owen Kelvin Avatar asked Oct 26 '25 12:10

Owen Kelvin


1 Answers

Laravel 8 changed the way factories work.

anyway, if you want to use old factory style in laravel 8, just install laravel legacy-factories

This package provides support for the previous generation of Laravel factories (<= 7.x) for Laravel 8.x+.

for more detail about this issue, see this github link.

like image 160
OMR Avatar answered Oct 29 '25 00:10

OMR