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();
        });
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 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

7年前 评论

@monkey 牛B,知道了!!!

7年前 评论

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