gpt4 book ai didi

php - Laravel 数据库 :seed always produces error "Object of class DateTime could not be converted to string"

转载 作者:行者123 更新时间:2023-12-02 21:27:25 27 4
gpt4 key购买 nike

每次数据库种子出现问题时,Laravel 4.1 似乎都会抛出异常“DateTime 类的对象无法转换为字符串”。

例如,假设我运行这个播种器:

use Faker\Factory as Faker;

class OrdersTableSeeder extends Seeder {

public function run()
{
$f = Faker::create();

foreach(range(1, 10) as $index)
{
$c = Customer::leftJoin('orders', function($join) {
$join->on('customers.id', '=', 'orders.customer_id');
})
->whereNull('orders.customer_id')
->first();

if (!empty($c)) {
Order::create([
'amazon_order_id' => rand(100, 999) . '-' . rand(1000000, 9999999) . '-' . rand(1000000, 9999999),
'customer_id' => $c->id,
'total' => rand(10, 100) . '.' . rand(1, 99),
'subtotal' => rand(10, 100) . '.' . rand(1, 99),
'first_name' => $c->first_name,
'last_name' => $c->last_name,
'address1' => $c->address1,
'address2' => $c->address2,
'address3' => $c->address3,
'city' => $c->city,
'state' => $c->state,
'county' => $c->county,
'district' => $c->district,
'postal_code' => $c->postalcode,
'country' => $c->country,
'ship_level' => array_rand(['Standard', 'Second', 'Next'], 1),
'status' => array_rand(['Shipped', 'Unshipped', 'Pending'], 1),
'order_date' => $f->dateTime,
'notes' => array_rand(['', $f->text(128)], 1)
]);
}
}
}

}

该播种器具有以下迁移 up() 函数:

public function up()
{
Schema::create('orders', function(Blueprint $table)
{
$table->increments('id');
$table->string('amazon_order_id', 19)->unique();
$table->unsignedInteger('customer_id')->references('id')->on('customers');
$table->decimal('total', 8, 2)->index();
$table->decimal('subtotal', 8, 2);
$table->string('first_name')->index();
$table->string('last_name')->index();
$table->string('address1');
$table->string('address2');
$table->string('address3');
$table->string('city');
$table->string('state');
$table->string('county');
$table->string('district');
$table->string('postal_code');
$table->string('country', 2);
$table->string('ship_level');
$table->string('status')->index();
$table->dateTime('order_date')->index();
$table->text('notes');
$table->timestamps();
});
}

这是 php artisan db:seed -vvv 的输出:

Exception trace:
() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Support\helpers.php:975
Illuminate\Exception\Handler->handleError() at n/a:n/a
preg_replace() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Support\helpers.php:975
str_replace_array() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Database\QueryException.php:53
Illuminate\Database\QueryException->formatMessage() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Database\QueryException.php:35
Illuminate\Database\QueryException->__construct() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Database\Connection.php:555
Illuminate\Database\Connection->run() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Database\Connection.php:338
Illuminate\Database\Connection->statement() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Database\Connection.php:295
Illuminate\Database\Connection->insert() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Database\Query\Processors\Processor.php:30
Illuminate\Database\Query\Processors\Processor->processInsertGetId() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php:1724
Illuminate\Database\Query\Builder->insertGetId() at n/a:n/a
call_user_func_array() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php:919
Illuminate\Database\Eloquent\Builder->__call() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1502
Illuminate\Database\Eloquent\Builder->insertGetId() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1502
Illuminate\Database\Eloquent\Model->insertAndSetId() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1472
Illuminate\Database\Eloquent\Model->performInsert() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1383
Illuminate\Database\Eloquent\Model->save() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:459
Illuminate\Database\Eloquent\Model::create() at C:\Ampps\www\mysite.dev\app\database\seeds\OrdersTableSeeder.php:41
OrdersTableSeeder->run() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Database\Seeder.php:37
Illuminate\Database\Seeder->call() at C:\Ampps\www\mysite.dev\app\database\seeds\DatabaseSeeder.php:25
DatabaseSeeder->run() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Database\Console\SeedCommand.php:52
Illuminate\Database\Console\SeedCommand->fire() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Console\Command.php:108
Illuminate\Console\Command->execute() at C:\Ampps\www\mysite.dev\vendor\symfony\console\Symfony\Component\Console\Command\Command.php:241
Symfony\Component\Console\Command\Command->run() at C:\Ampps\www\mysite.dev\vendor\laravel\framework\src\Illuminate\Console\Command.php:96
Illuminate\Console\Command->run() at C:\Ampps\www\mysite.dev\vendor\symfony\console\Symfony\Component\Console\Application.php:892
Symfony\Component\Console\Application->doRunCommand() at C:\Ampps\www\mysite.dev\vendor\symfony\console\Symfony\Component\Console\Application.php:191
Symfony\Component\Console\Application->doRun() at C:\Ampps\www\mysite.dev\vendor\symfony\console\Symfony\Component\Console\Application.php:121
Symfony\Component\Console\Application->run() at C:\Ampps\www\mysite.dev\artisan:59

这里可能发生了什么?考虑到我什至没有显式添加 DateTime 对象...除了 Faker 的“order_date”的 dateTime 之外,我无法立即发现它有任何问题,但这应该不是问题。

请注意,customers 表已创建,并比该表提前 1 步进行播种。

这是 Laravel 4.1 的错误吗?

最佳答案

MySQL DATETIME 列类型需要 String。 Faker 正在返回 PHP DateTime 对象。

格式化 Faker 的 DateTime 对象以返回 String:

Order::create([
// Omitted fields above

'order_date' => $f->dateTime->format('Y-m-d H:i:s'),

// Omitted fields below
]);

关于php - Laravel 数据库 :seed always produces error "Object of class DateTime could not be converted to string",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23178093/

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