创建Laravel 项目
配置项目
- Apache 服务器 虚拟主机配置 和 本地域名解析
- 安装LaravelDebugbar
- 配置环境变量中的数据库连接参数 项目根目录 下的 .env 文件
- config文件目录下,修改 项目应用 app.php文件,修改 时区 'timezone' => 'UTC', 为 'timezone' => 'Asia/Shanghai',
开启登录注册功能
使用 php artisan 命令来 开启
php artisan 查看artisan 命令
php artisan route:list 列出当前注册的访问路径
php artisan make:auth 自动添加注册登陆所需要的访问路径和视图 route 和 view
但是这样生成的,只有 路由和 视图,并没有功能,数据库还没创建不是!完成后,再访问下:
auth
创建数据模型 Eloquent ORM (Object Relation Mapping)
// 项目根目录下运行
php artisan make:model -h 查看帮助
php artisan make:model Project -m 创建数据模型,带 -m 同事创建数据迁移文件
php artisan make:model Task -m
artisanMakeModel.png
定义数据模型之间的关系 Eloquent:relationships
EloquentRelationships(Xmind)
User.php 模型文件 添加两个函数 来建立之间的关系
/**
* Gets the number of items for the current user
*/
public function project ()
{
// use $user->project(); Gets the number of current user items
return $this->hasMany('App\Project');
}
/**
* Gets the number of tasks for the current user to get through the project
*/
public function tasks ()
{
// use $user->tasks();
retrun $this->hasManyThrough('App\Task','App\Project');
}
Project.php
/**
* Gets the current items belonging to which user
*/
public function User ()
{
// use $project->user();
return $this->belongTo('App\User');
}
/**
* Gets the number of tasks under the current itmes
*/
public function tasks ()
{
// use $project->tasks();
return $this->hasMany('App\Task');
}
Task.php
/**
* Gets the current task which belongs to the project
*/
public function project ()
{
// use $task->project();
return $this->belongTo('App\Project');
}
当前项目用到两种关系:hasMany 有多少个 、 hasManyThrough 有多少个 通过 谁
数据表结构设计与迁移 (database migration)
1、数据库迁移文件(数据表蓝图文件)
2、创建相应的蓝图文件,通过命令就可以创建相应的数据表
3、对蓝图的修改,再通过命令就可以相应更新数据表
相当于拥有版本控制的动能
php artisan 查看artisan 相关命令,在migrate 下是数据表相关命令
php artisan migrate:status 状态
php atrisan migrate 创建数据表,根据数据蓝图创建
database 目录下/ migrations 迁移文件
对 Project 和 Task 两张蓝图进行设计 (设计文件)
Schema::create('porjects', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('thumbnail'); // 缩略图
$table->integer('user_id');
$table->timestamps();
});
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->integer('project_id');
$table->boolean('completed'); // 任务完成状态
$table->timestamps();
});
使用的命令
php artisan migrate:rollback // 回滚 回到之前命令 前的状态
php artisan migrate