【发布时间】:2021-03-30 17:20:52
【问题描述】:
最近升级到 Laravel 8 但现在我收到警告
方法“loadFactoriesFrom”已弃用
我的文件结构如下
myapp/
├── packages/
│ ├── package-1/
| | |── src/
| | | |── databases/
| | | | |── factories
| | | | | |── PackageModel11
| | | | | |── PackageModel12
| | | | |── Package1ServiceProvider
| | | | └── migrations
| | | |── models/
| | | |── controllers/
| | | |── migrations/
| | | └── /
| | |── tests/
| | |── composer.json/
│ ├── app
│ ├── database/
│ ├── resources/
│ └── public/
为了加载我的自定义包,我使用了各种函数
<?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
}
}
既然loadFactoriesFrom 已被弃用,我该如何加载这些工厂?
更新
我在这里找到了connect models and Factories 的方法,他们记录了覆盖newFactory() 函数。
所以我的测试中有以下内容
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);
}
我的工厂
class ELearningTopicFactory extends Factory
{
public function definition()
{
return [
'name' => 'Some name'
];
}
}
还有我的模特
use HasFactory;
protected static function newFactory()
{
return ELearningTopicFactory::new();
}
现在我在下面出现错误
找不到类“App\ELearningTopic”
我会错过什么?为什么没有覆盖模型类
【问题讨论】: