Laravel 中的用户认证(Auth)以及数据迁移与填充

18 篇文章 0 订阅

1. 生成 Auth 所需文件;

  • 在 Artisan 控制台输入命令:php artisan make:auth
# 进入项目目录中 
cd /data/project/test/laravel1

# 生成
php artisan make:auth
# 返回: Authentication scaffolding generated successfully.
  • 此时在 routes/web.php 会自动生成以下代码
// 路由路径在 vendor/laravel/framework/src/routing/Illuminate/routing/router.php
// 锁定方法 public function auth()
Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
  • resources/views/ 目录以及 App/Http/Controllers/ 都会生成相关文件
    在这里插入图片描述在这里插入图片描述
  • 访问:http://192.168.1.214:2000/home
  • 此页面提供了登录、注册、找回密码等功能
    在这里插入图片描述
  • 生成此页面功能对应的表,需要使用迁移功能
  • database/migrations/ 下存放数据迁移文件
# 执行迁移
php artisan migrate
# 返回:
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table

2. 数据迁移;

  • 案例:新建一个 student 表的迁移文件
# 新建学生表
CREATE TABLE IF NOT EXISTS students(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '姓名', 
`age` TINYINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '年龄',
`gender` TINYINT UNSIGNED NOT NULL DEFAULT 10 COMMENT '性别', 
`created_at` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
`updated_at` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '修改时间'
) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1001 COMMENT='学生表';

# --table 和 --create 参数可以用来指定数据表名称,以及迁移文件是否要建立新的数据表
php artisan make:migration create_students_table --create=students
# 返回:Created Migration: 2019_08_18_071551_create_students_table

# 修改迁移文件 database/migrations/2019_08_18_071551_create_students_table.php
# 完善 up() 方法,补全字段
public function up()
{
    Schema::create('students', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->integer('age')->unsigned()->default(0);
        $table->integer('gender')->default(10);
        $table->integer('created_at')->default(0);
        $table->integer('updated_at')->default(0);
        // $table->timestamps();
    });
}

# 执行迁移
php artisan migrate

  • 生成模型的同时生成迁移文件
php artisan make:model Article -m

3. 数据填充。

  • 创建一个填充文件,并完善填充文件
php artisan make:seeder StudentTableSeeder
# 返回: Seeder created successfully.

# 完善填充文件 database/seeds/StudentTableSeeder.php
# 完善 run() 方法
# 在 run() 方法中可以插入任何想插入的数据,比如查询构造器、手动插入,也可以使用 Eloquent 模型工厂
public function run()
{
    DB::table('students')->insert([
        ['name' => 'test1', 'age' => 18],
        ['name' => 'test2', 'age' => 19],
    ]);
}

  • 执行单个填充文件
php artisan db:seed --class=StudentTableSeeder
# 返回: Database seeding completed successfully.
  • 批量执行填充文件
# 修改 database/seeds/DatabaseSeeder.php 中 run() 方法
public function run()
{
     $this->call(StudentTableSeeder::class);
}

# 执行
php artisan db:seed
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值