Laravel 5.2 用 tinker 生成测试数据报错

file

求大神指教为什么会报错!

详细信息:

ModelFactory.php 中代码如下:

$factory->define(App\Models\Notice::class, function (Faker\Generator $faker) {
    return [
        'title' => $faker->sentences,
        'content' => $faker->paragraph,
    ];
});

模型 Notice.php 之下了如下代码

我的model是在app\Models目录下的;我是用 php artisan make model:Models/Notice 生成的

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Notice extends Model
{
    protected $table = 'notice';
    protected $fillable = ['title','content'];
}

migration 如下:

Schema::create('notice', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('content');
            $table->integer('user_id')->default('0');
            $table->timestamps();
        });
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 2
monkey

问题原因

错误提示已经描述的很清楚了

Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given...

即原本输入应该是字符类型,结果却输入成了数组类型。

解决方案

ModelFactory.php 文件中的

    return [
        'title' => $faker->sentences,
        ......
    ];

修改成

    return [
        'title' => $faker->sentence,
        ......
    ];

对,就是去掉了一个 's' 字符,官方文档里有解释这个 sentences 的用法:

file

返回的是 array 类型的哦。

更多的 faker 用法可以参考官方文档哦:https://github.com/fzaninotto/Faker

8年前 评论

@monkey 牛B,知道了!!!

8年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!