gpt4 book ai didi

php - 我在进行数据库播种时出现 InvalidArgumentException

转载 作者:行者123 更新时间:2023-11-29 07:17:27 24 4
gpt4 key购买 nike

我正在研究 Restful API,当我想在数据库中播种假数据时出现异常消息。

  • 我更新数据库。
php artisan migrate:fresh 
  • 我在数据库上播种。
php artisan db:seed 

我用迁移和 Controller 制作模型:

  • 迁移:000000_create_posts_table.php
public function up()
{
Schema::create('posts', function (Blueprint $table) {


$table->bigIncrements('id');
$table->string('title');
$table->text('content');

$table->dateTime('date_written');

$table->String('feature_image')->unllable();
$table->integer('votes_up')->unllable();
$table->integer('votes_down')->unllable();

// TelationShipe.
$table->integer('user_id');
$table->integer('category_id');

$table->timestamps();
});



  • 模型:Post.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
protected $fillable = [
'title' , 'content' , 'date_written' ,
'feature_image' , 'votes_up' ,
'votes_down' , 'user_id' ,
'category_id'
];
}
  • Controller :PostController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{
//
}
  • 播种器:DatabaseSeeder.php
<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
factory(\App\Post::class , 100 )->create();
}
}
  • 工厂:PostFactory.php
<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/

$factory->define(User::class, function (Faker $faker) {
return [
'title' => $faker->title,
'content' => $faker->text(400),
'date_written' => $faker->new(),
'feature_image' => $faker->imageUrl(),
'votes_up' => $faker->numberBetween(1 , 100),
'votes_down' => $faker->numberBetween(1 , 100),
'user_id' => $faker->numberBetween(1 , 15),
'category_id' => $faker->numberBetween(1 , 15),
];
});
  • 但实际的输出控制台:

InvalidArgumentException:未知格式化程序"new"在

  ~/vendor/fzaninotto/faker/src/Faker/Generator.php:242
238|
239| return $this->formatters[$formatter];
240| }
241| }
242| throw new \InvalidArgumentException(sprintf('Unknown formatter "%s"', $formatter));
243| }
244|
245| /**
* Replaces tokens ('{{ tokenName }}') with the result
* from the token method call
*/

Exception trace:

1 Faker\Generator::getFormatter("new")
~/vendor/fzaninotto/faker/src/Faker/Generator.php:222

2 Faker\Generator::format("new", [])
~/vendor/fzaninotto/faker/src/Faker/Generator.php:279

Please use the argument -v to see more details.

最佳答案

改变这一行

来自

'date_written' => $faker->new(),

'date_written' => now(),

now()将返回数据库迁移所需的当前时间的 Carbon 实例

There's no such function on faker generator called new

希望对你有帮助

关于php - 我在进行数据库播种时出现 InvalidArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58465493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com