数据迁移设置字符段
数据填充设置数据内容
数据库迁移
生成迁移
使用 Artisan 命令 make:migration 来创建迁移:
php artisan make:migration create_users_table
新的迁移文件会被放置在 database/migrations 目录中。每个迁移文件的名称都包含了一个时间戳,以便让 Laravel 确认迁移的顺序。
- –table 选项可用来指定数据表的名称
- –create该迁移被执行时是否将创建的新数据表。
这些选项需在预生成迁移文件时填入指定的数据表:
php artisan make:migration create_users_table --create=users
php artisan make:migration add_votes_to_users_table --table=users
迁移结构
迁移类通常会包含两个方法:up 和 down。up 方法可为数据库添加新的数据表、字段或索引,而 down 方法则是 up 方法的逆操作。
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFlightsTable extends Migration
{
/**
* 运行数据库迁移
*
* @return void
*/
public function up()
{
Schema::create('flights', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('airline');
$table->timestamps();
});
}
/**
* 回滚数据库迁移
*
* @return void
*/
public function down()
{
Schema::drop('flights');
}
}
运行迁移
使用 Artisan 命令 migrate 来运行所有未完成的迁移:
php artisan migrate
在生产环境强制执行迁移
一些迁移操作是具有破坏性的,这意味着可能会导致数据丢失。为了防止有人在生产环境中运行这些命令,系统会在这些命令被运行之前与你进行确认。如果要强制忽略系统的提示运行命令,则可以使用 --force 标记:
php artisan migrate --force
回滚迁移
若要回滚最后一次迁移,可以使用 rollback 命令。此命令将回滚最后一次「迁移」,其中可能包含多个迁移文件:
php artisan migrate:rollback
在 rollback 命令后加上 step 参数,你可以限制回滚迁移的个数。例如,以下命令将回滚最近五次迁移:
php artisan migrate:rollback --step=5
migrate:reset 命令可以回滚应用程序中的所有迁移:
php artisan migrate:reset
使用单个命令来执行回滚或迁移
migrate:refresh 命令不仅会回滚数据库的所有迁移还会接着运行 migrate 命令。这个命令可以高效地重建整个数据库:
php artisan migrate:refresh
// 刷新数据库结构并执行数据填充
php artisan migrate:refresh --seed
使用 refresh 命令并提供 step 参数来回滚并再执行最后指定的迁移数。例如,以下命令将回滚并重新执行最后五次迁移:
php artisan migrate:refresh --step=5
删除所有表 & 迁移
migrate:fresh 命令会从数据库中删除所有表,然后执行 migrate 命令:
php artisan migrate:fresh
php artisan migrate:fresh --seed
数据库填充
创建注入器类文件
配置(database/seeder/DatabaseSeeder.php配置)
$this->call([
UsersTableSeeder::class,
]);
运行命令
php artisan db:seed
php artisan db:seed --class=UsersTableSeeder
简介
Laravel 可以用 seed 类轻松地为数据库填充测试数据。所有的 seed 类都存放在 database/seeds
目录下。你可以任意为 seed 类命名,但是更应该遵守类似 UsersTableSeeder 的命名规范。Laravel 默认定义的一个
DatabaseSeeder 类。可以在这个类中使用 call 方法来运行其它的 seed 类从而控制数据填充的顺序。
- 编写seeders
通过运行 Artisan 命令 make:seeder 来生成 Seeder。由框架生成的 seeders 都将被放置在 database/seeds 目录下:
php artisan make:seeder UsersTableSeeder
- 使用模型工厂
- 调用其他seeders
- 运行seeders
模型
一个类对应一张表
一个对象对应一条记录
User类对应的是user表
$user=new User;
$user对象对应表中的一条记录
php artisan make:model Comment -m
模型约定
创建Comments和数据迁移的数据表(php artisan migrate creat_table_comments及其基本结构
- 默认主键名字是id
- 类名为单数。表名为复数
- 默认有时间戳,没有改成falsecreated_at,updated_at
- 外键命名格式:主表单数_id
- 主表的外键id要等于主表名字的单数加_id
- 关于多对多的中间表名称的规则:用两个模型的单数拼接起来创建(按照首字母顺序排序如lesson_user)
模型关联
添加字段时
php artisan make: migration add_group_id_to_users --table="users"
php artisan make:migration tian_jia_group_id_dao_users --table="users"
添加group_id字段给users
流程:
- 数据迁移新建表
up里面添加用户字段 - 填充表
- (多对多关系)新建中间表
$table->integer('user_id')->comment('用户id');
$table->integer('lesson_id')->comment('课程id');
- 连接关系
创建子表的方法,返回主表(用户类)将和子表产生什么关系(…)
return $this->hasOne('App\Userinfo');
- 控制器查询验证
用子表的方法进行连接
$group=$user->group->name;
- 一对一(比如用户和用户的详细信息) 配置关系
public function userinfo(){
// 用户类将和哪个类产生一对一的关系
return $this->hasOne('App\Userinfo');
}
- 一对多(比如一个人写了多篇文章)
/*
* 一对多关系
* */
public function news(){
// 用户类将和哪个类产生一对一的关系
return $this->hasMany('App\News');
}
- 一对多(反向)(属于关系,比如多个人属于一个组)
public function group(){
// 用户类将和哪个类产生一对一的关系
return $this->belongsTo('App\Group');
}
- 多对多
#关于动态属性和动态的方法实现原理
__get
__call
__callStatic
#laravel框架是由一个容器组合而成的
// 多对多关系
$lession =$user->lessons; //__get
/*
* return $this->getStribute($key)
*魔术方法
* */
dd($lession);
//类名不区分大小写,函数名也不区分
// $lession=Lesson::find(3);
// $users=$lession->users->count();
// dd($users);
一对一关系MVC
模型
public function userinfo(){
// 用户类将和哪个类产生一对一的关系
return $this->hasOne('App\Userinfo');
}
控制器
$user=User::where('id',2)->with(['userinfo'])->first();
// dd($user);
// $info=$user->userinfo;
// $xingzuo=$info->xingzuo;
// dd($info);
return view('test/test',compact('user'));
前端
{{$user->name}}
{{$user->userinfo->xingzuo}}
//userinfo不用括号
GIT
目前世界上最先进的分布式版本控制系统 git服务器是提供开发者“交换”代码,服务器代码丢了没关系,分分钟再建一台
工作区->(缓存区->版本库)版本库和远程仓库交互
-
先加入到工作区,git init
-
添加所有文件到缓存区,git add .
-
查看状态,git status
-
git commit -m “初始化”(-m添加注释)
-
在远程创建一个远程仓库
git remote add origin https://gitee.com/ceonjinlau/xxx.git巴拉巴拉
(origin是别名) -
将本地仓库推送到远程连接
git push -u origin master(master是分支,-u建立本地和远程分支的关联) -
查看log,git log --pretty=oneline/git log
git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .env.example
new file: .gitattributes
new file: .gitignore
new file: app/Console/Kernel.php
new file: app/Exceptions/Handler.php
new file: app/Group.php
new file: app/Http/Controllers/Auth/ForgotPasswordController.php
new file: app/Http/Controllers/Auth/LoginController.php
new file: app/Http/Controllers/Auth/RegisterController.php
new file: app/Http/Controllers/Auth/ResetPasswordController.php
new file: app/Http/Controllers/Controller.php
new file: app/Http/Controllers/CookieController.php
new file: app/Http/Controllers/MemberController.php
git init
让文件夹和git联系
[master (root-commit) 816efeb] 鍒濆鍖? 280 files changed, 32614 insertions(+)
create mode 100644 .env.example
create mode 100644 .gitattributes
create mode 100644 .gitignore
create mode 100644 app/Console/Kernel.php
create mode 100644 app/Exceptions/Handler.php
create mode 100644 app/Group.php
git add 文件名,添加到暂存区
很多文件git add . 添加所有文件
创建分支 git branch user
切换分支 git checkout user
创建并切换分支 git checkout -b list
合并分支 git merge user
查看有哪些分支,并拉取到本地 git fetch
查看是否冲突 git diff
提交操作:
git add
git commit -m “用户模块"
git push -u origin master