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();

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

  • 相关阅读:
    c语言指针应用总结
    C语言指针知识点
    标准输出scanf函数
    C语言32个关键字查询
    kali2020 无法使用arpspoof ,切换阿里云源进行安装
    “中国网络安全能力图谱”发布,安华金和当选数据安全领域代表者!
    【官方文档】-Windows Server 安装和升级
    SQL Server 2012配置Always On可用性组
    【官方文档】-SQL Server 的最大容量规范
    【官方文档】-按 SQL Server 版本划分的计算能力限制
  • 原文地址:https://www.cnblogs.com/Alex-sk/p/6654257.html
Copyright © 2011-2022 走看看