我正在使用 Lumen 8.3,想factory()在我的測驗中使用函式,它給了我
Undefined Function,Lumen Docs 中沒有任何用處
我在這里錯過了什么嗎?
class ProductTest extends TestCase
{
public function test_if_can_send_products_list(){
$products = factory('App/Products',5)->make();
$this->json('post','/payouts',$products)
->seeJson([
'created' => true,
]);
}
}
->
Error: Call to undefined function factory()
uj5u.com熱心網友回復:
最好使用這樣的直接類:
$products = factory(Products::class, 5)->create();
不要忘記添加Products模型用法(命名空間)。
編輯
您應該創建工廠:
<?php
namespace Database\Factories;
use App\Products;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class ProductFactory extends Factory
{
protected $model = Products::class;
public function definition(): array
{
return [
'name' => $this->faker->unique()->userName()
];
}
}
并將HasFactoryTrait添加到您的模型中:
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Products extends Model {
use HasFactory;
}
你也可以這樣使用
Products::factory()->count(5)->make();
uj5u.com熱心網友回復:
我只是在app.php檔案中取消注釋這些行
$app->withFacades();
$app->withEloquent();
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/386362.html
