Laravel ORM 数组到字符串的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39359130/
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 ORM Array to String Conversion
提问by Mark Karavan
Laravel 5.3, PHP 5.6
Fresh laravel new
project, minimal configuration.
Laravel 5.3,PHP 5.6 Freshlaravel new
项目,最少配置。
I have made a simple migration and model, and am trying to seed data into it via php artisan tinker
.
我做了一个简单的迁移和模型,并试图通过php artisan tinker
.
My migration looks like this:
我的迁移如下所示:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatesFooTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('foo', function (Blueprint $table) {
$table->increments('id');
$table->string('foo', 20);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('foo');
}
}
When I run php artisan migrate
the database populates just fine.
当我运行php artisan migrate
数据库填充就好了。
The corresponding model is simple:
对应的模型很简单:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Foo extends Model
{
protected $table = 'foo';
protected $fillable = ['foo'];
}
I have a ModelFactory as well:
我也有一个 ModelFactory:
$factory->define(App\Foo::class, function (Faker\Generator $faker) {
return [
'foo' => $faker->email,
];
});
Tinker does what I think it should do when I try to make
a flyer from the factory:
当我尝试make
从工厂制作传单时,Tinker 做了我认为应该做的事情:
>>> factory('App\Foo')->make();
=> App\Foo {#696
foo: "[email protected]",
But when I try to hit the database, Eloquent fails to wrap the query values in a string:
但是当我尝试访问数据库时,Eloquent 无法将查询值包装在字符串中:
>>> $foo = factory('App\Foo')->create();
Illuminate\Database\QueryException with message 'SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'foo' at row 1 (SQL: insert into `foo` (`foo`, `updated_at`, `created_at`) values ([email protected], 2016-09-06 23:59:03, 2016-09-06 23:59:03))'
Nothing like this on Google. Any ideas?
谷歌上没有这样的东西。有任何想法吗?
(Edited to show the same problem with a much simpler example)
(编辑以使用更简单的示例显示相同的问题)
回答by haakym
Some of the faker methods return their result as an array, as a default. For example:
默认情况下,一些 faker 方法将其结果作为数组返回。例如:
$faker->words(3)
would return:
会返回:
array('porro', 'sed', 'magni')
To return the result as a string you can pass true
as the second argument for some methods, as in the previous example using the words
method:
要将结果作为字符串返回,您可以true
作为某些方法的第二个参数传递,如使用该words
方法的前一个示例:
$faker->words(3, true)
returns:
返回:
"porro sed magni"
As described in the readme:
如自述文件中所述:
words($nb = 3, $asText = false) // array('porro', 'sed', 'magni')
回答by Mark Karavan
Solved it. The problem was the faker library. Apparently when you explicitly cast (string)$faker->whatever
it fixes the problem.
解决了。问题是伪造的图书馆。显然,当你明确地投射(string)$faker->whatever
它时,问题就解决了。