Mar*_*van 6 php orm laravel eloquent
Laravel 5.3,PHP 5.6新laravel new项目,最小配置.
我做了一个简单的迁移和模型,并试图通过它将数据播种到其中php artisan tinker.
我的迁移看起来像这样:
<?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');
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行php artisan migrate数据库时,填充就好了.
相应的模型很简单:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Foo extends Model
{
protected $table = 'foo';
protected $fillable = ['foo'];
}
Run Code Online (Sandbox Code Playgroud)
我也有一个ModelFactory:
$factory->define(App\Foo::class, function (Faker\Generator $faker) {
return [
'foo' => $faker->email,
];
});
Run Code Online (Sandbox Code Playgroud)
当我尝试make从工厂发传单时,Tinker做了我认为应该做的事情:
>>> factory('App\Foo')->make();
=> App\Foo {#696
foo: "morgan39@gmail.com",
Run Code Online (Sandbox Code Playgroud)
但是当我尝试命中数据库时,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 (jakayla62@streich.org, 2016-09-06 23:59:03, 2016-09-06 23:59:03))'
Run Code Online (Sandbox Code Playgroud)
谷歌上没有这样的东西.有任何想法吗?
(用更简单的例子编辑显示同样的问题)
haa*_*kym 11
一些faker方法将其结果作为数组返回,作为默认值.例如:
$faker->words(3)
Run Code Online (Sandbox Code Playgroud)
会回来:
array('porro', 'sed', 'magni')
Run Code Online (Sandbox Code Playgroud)
要将结果作为字符串返回,您可以将其true作为某些方法的第二个参数传递,如上一个示例中使用该words方法:
$faker->words(3, true)
Run Code Online (Sandbox Code Playgroud)
收益:
"porro sed magni"
Run Code Online (Sandbox Code Playgroud)
如自述文件中所述:
words($nb = 3, $asText = false) // array('porro', 'sed', 'magni')
Run Code Online (Sandbox Code Playgroud)