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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With