zoukankan      html  css  js  c++  java
  • Laravel5.1 搭建简单的社区(二)--创建数据

    声明:此篇笔记记录的是laravist网站上的视频教程,有兴趣可以去逛逛。

    在做好准备工作后我们就来创建数据表和数据,首先先创建discussions表:

    php artisan make:migration create_discussions_table --create=discussions
        public function up()
        {
            Schema::create('discussions', function (Blueprint $table) {
                $table->increments('id');
                $table->string('title');        // 帖子的标题
                $table->text('body');           // 帖子的内容
                $table->integer('user_id')->unsigned();     // 这篇帖子的作者是谁
                $table->integer('last_user_id')->unsigned();     // 这篇帖子最后是由谁更新的
    
                // 声明user_id外键
                $table->foreign('user_id')
                      ->references('id')
                      ->on('users')
                      ->onDelete('cascade');
                $table->timestamps();
            });
        }

    修改users表的迁移文件,新增一个avatar字段:

        public function up()
        {
            Schema::create('users', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name');
                $table->string('avatar');
                $table->string('email')->unique();
                $table->string('password', 60);
                $table->rememberToken();
                $table->timestamps();
            });
        }

    执行migrate,创建discussion模型:

    php artisan migrate
    php artisan make:model Discussion

    使用factory创建user测试数据

    先进入User模型中,在fillable(白名单)数组中添加我们新加入的字段 avatar:

    protected $fillable = ['name', 'email', 'password', 'avatar'];

    进入ModelFactory.php中修改创建User模型的方法:

    $factory->define(AppUser::class, function ($faker) {
        return [
            'name' => $faker->name,
            'email' => $faker->email,
            // 添加avatar的生成方法
            'avatar' => $faker->imageUrl(256,256),
            'password' => str_random(10),
            'remember_token' => str_random(10),
        ];
    });

    进入tinker中批量生成用户:

    php artisan tinker               
    Psy Shell v0.7.2 (PHP 5.6.25 — cli) by Justin Hileman
    >>> factory('AppUser',10)->create();

    查看数据库,是否生成了数据。

    使用factory创建discussion测试数据

    在Discussion模型中创建白名单数组:

    class Discussion extends Model
    {
        protected $fillable = ['title', 'body', 'user_id', 'last_user_id'];
    }

    在ModelFactory创建新的方法:

    $factory->define(AppDiscussion::class, function ($faker) {
        // lists()方法是列出某一列的值
        $user_id_array = AppUser::lists('id')->toArray();
        return [
            'title' => $faker->sentence,
            'body' => $faker->paragraph,
            'user_id' => $faker->randomElement($user_id_array),
            'last_user_id' => $faker->randomElement($user_id_array),
        ];
    });

    在tinker中批量生成数据:

    php artisan tinker
    Psy Shell v0.7.2 (PHP 5.6.25 — cli) by Justin Hileman
    >>> factory('AppDiscussion',30)->create();

    查看数据库,是否生成了数据。

  • 相关阅读:
    (网页)中的简单的遮罩层
    (后端)shiro:Wildcard string cannot be null or empty. Make sure permission strings are properly formatted.
    (网页)jQuery的时间datetime控件在AngularJs中使用实例
    Maven Myeclipse 搭建项目
    MyBatis 环境搭建 (一)
    java 常用方法
    XML 基础
    JS BOM
    js 事件
    js 的使用原则
  • 原文地址:https://www.cnblogs.com/Alex-sk/p/6654257.html
Copyright © 2011-2022 走看看