php - 未定义的方法数据库\工厂\ Laravel 8
问题描述
我对工厂和种子的怀疑。我尝试使用种子进行迁移,但遇到以下问题。Call to undefined method Database\Factories\PostFactory::faker()
我不太清楚在哪里拨打电话,如果在一个单独的文件中,比如 PostSeeds 或者我在这里是如何做的。
这是主文件 DatabaseSeeder.php
<?php
namespace Database\Seeders;
use App\Models\Category;
use App\Models\Post;
use App\Models\User;
use Illuminate\Support\Facades\Storage;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
// \App\Models\User::factory(10)->create();
Storage::makeDirectory('posts');
User::create([
'name' => 'Fernando',
'email' => 'cainuriel@gmail.com',
'password' => '$2y$10$.CjCfp.mzo.AUMPZj0n7mOK3zU6QmrabKXwaYcHNFtK1qWHRqf4Xe',
]);
Category::create([
'name' => 'Plagas',
'slug' => 'plagas',
]);
Category::create([
'name' => 'Enfermedades plantas',
'slug' => 'enfermedades-plantas',
]);
Category::create([
'name' => 'Cultivo ecológico',
'slug' => 'cultivo-ecologico',
]);
Category::create([
'name' => 'Consciencia ecológica',
'slug' => 'consciencia-ecologica',
]);
Post::factory(10)->create();
}
}
这是我做fakers数据的主文件My Postfactory.php。
<?php
namespace Database\Factories;
use App\Models\Category;
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class PostFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Post::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$name = $this->faker->unique()->sentence();
return [
'name' => $name,
'slug' => Str::slug($name),
'extract' => $this->faker->text(250),
'body' => $this->faker->text(2000),
'status' => 2,
'category_id' => Category::all()->random()->id,
'user_id' => 1,
'url_image' => 'posts'.$this->faker('public/storage/posts', 650, 490, null, false)
];
}
}
我不知道用表结构显示迁移是否也有用
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->text('extract');
$table->longtext('body');
$table->enum('status', [1, 2])->default(1); // 1. Borrador. 2. Publicar.
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('category_id');
$table->string('url_image')->default('default.png');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
解决方案
请验证您use HasFactory
在 Post 模型中是否具有特征,并且当您使用 laravel 8 时,请将url_image
Postfactory.php 中的替换为以下内容:
'url_image' => $this->faker->image('public/storage/posts', 650, 490, null, false)
推荐阅读
- python-3.x - 如何根据以下代码创建相似度矩阵?
- regex - 正则表达式中的正则表达式或匹配中的模式或在两个字符串之间查找逗号
- delphi - 使用 LockBox 验证 RSA 签名
- azure - 使用 AZ CLI 创建 Azure 应用注册时出现问题
- python - 在机器学习中使用 numpy.newaxis
- java - Comparator .comparing().reversed() 奇怪的行为/没有按预期工作
- concurrency - Spring JPA @QueryHint 因锁定超时而被忽略?
- c# - 在 npgsql 准备命令中替换准备好的查询参数的问题
- c# - 需要帮助编辑代码以在键盘而不是滚轮上旋转
- javascript - 如何在 three.js 中修复 FirstPersonControls 给出错误 targetPosition.setFromSphericalCoords is not a function