类 DateTime 的 laravel 工匠种子对象无法转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22391720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel artisan seed Object of class DateTime could not be converted to string
提问by user2075215
I get an error when running (in laravel)
运行时出现错误(在 Laravel 中)
php artisan migrate --seed
I get
我得到
Object of class DateTime could not be converted to string
My Users seed table looks like this
我的用户种子表如下所示
class UsersTableSeeder extends Seeder
{
public function run()
{
DB::table('users')->delete();
$users = [
[
'firstname' => 'David',
'surname' => 'Smith',
'email' => '[email protected]',
'created_at' => new DateTime,
'updated_at' => new DateTime
],
[
'firstname' => 'Mike',
'surname' => 'Smith',
'email' => '[email protected]',
'created_at' => new DateTime,
'updated_at' => new DateTime
]
];
DB::table('users')->insert($users);
}
}
Any ideas why artisian/php is complaining?
任何想法为什么工匠/ php 抱怨?
回答by marcanuy
You can use the same package that Laravel uses https://github.com/briannesbitt/Carbonfor managing dates, to fix that, fill datetime columns with appropriate values:
您可以使用 Laravel 使用https://github.com/briannesbitt/Carbon管理日期的相同包,以解决该问题,用适当的值填充日期时间列:
class UsersTableSeeder extends Seeder
{
public function run()
{
DB::table('users')->delete();
$dt = Carbon::now();
$dateNow = $dt->toDateTimeString();
$users = [
[
'firstname' => 'David',
'surname' => 'Smith',
'email' => '[email protected]',
'created_at' => $dateNow,
'updated_at' => $dateNow
],
[
'firstname' => 'Mike',
'surname' => 'Smith',
'email' => '[email protected]',
'created_at' => $dateNow,
'updated_at' => $dateNow
]
];
DB::table('users')->insert($users);
}
}
回答by user2983930
"New DateTime" does work without a problem in Laravel, apparently something has been added to Laravel that lets it overcome this error.
“New DateTime”在 Laravel 中确实可以正常工作,显然 Laravel 中已经添加了一些东西来克服这个错误。
What I've seen happen, though, is that there are other errors in the Seed data, such as specifying a field that doesn't exist, or specifying two seeds that have different columns, and the "DateTime" error appears by default. Try temporarily removing your created_at and updated_at fields from the seed and Laravel will reveal what the actual problem is.
但是,我所看到的情况是,种子数据中存在其他错误,例如指定不存在的字段,或指定具有不同列的两个种子,并且默认情况下会出现“DateTime”错误。尝试从种子中暂时删除 created_at 和 updated_at 字段,Laravel 将揭示实际问题所在。
回答by Alen
DateTime
return time and you need string, so to get time in string format try something like this:
DateTime
返回时间,您需要字符串,因此要以字符串格式获取时间,请尝试以下操作:
$newDate = DateTime::createFromFormat("l dS F Y", $someDate);
$newDate = $newDate->format('d/m/Y');
Change formating as you need it.
根据需要更改格式。
回答by Rick
The new DateTime()
function works perfectly with laravel make sure that the proper value is set for the table name in app/models/tablename.php
该new DateTime()
函数与 laravel 完美配合,确保为 app/models/tablename.php 中的表名设置了正确的值
protected $table = 'TableName';
Another note, the class is always singular and the tables tend to be plural for example PHP class
另一个注意事项,类总是单数,表格往往是复数,例如 PHP 类
user.php
Table Name
表名
Users
Lastly make sure that the key in the array is correct
最后确保数组中的键是正确的
'updated_at' vs 'updated_at '<--Note the empty space