創(chuàng)建Laravel 項目
配置項目
- Apache 服務(wù)器 虛擬主機配置 和 本地域名解析
- 安裝LaravelDebugbar
- 配置環(huán)境變量中的數(shù)據(jù)庫連接參數(shù) 項目根目錄 下的 .env 文件
- config文件目錄下者娱,修改 項目應(yīng)用 app.php文件抡笼,修改 時區(qū) 'timezone' => 'UTC', 為 'timezone' => 'Asia/Shanghai',
開啟登錄注冊功能
使用 php artisan 命令來 開啟
php artisan 查看artisan 命令
php artisan route:list 列出當(dāng)前注冊的訪問路徑
php artisan make:auth 自動添加注冊登陸所需要的訪問路徑和視圖 route 和 view
但是這樣生成的,只有 路由和 視圖肺然,并沒有功能蔫缸,數(shù)據(jù)庫還沒創(chuàng)建不是!完成后际起,再訪問下:
auth
創(chuàng)建數(shù)據(jù)模型 Eloquent ORM (Object Relation Mapping)
// 項目根目錄下運行
php artisan make:model -h 查看幫助
php artisan make:model Project -m 創(chuàng)建數(shù)據(jù)模型拾碌,帶 -m 同事創(chuàng)建數(shù)據(jù)遷移文件
php artisan make:model Task -m
artisanMakeModel.png
定義數(shù)據(jù)模型之間的關(guān)系 Eloquent:relationships
EloquentRelationships(Xmind)
User.php 模型文件 添加兩個函數(shù) 來建立之間的關(guān)系
/**
* 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');
}
當(dāng)前項目用到兩種關(guān)系:hasMany 有多少個 吐葱、 hasManyThrough 有多少個 通過 誰
數(shù)據(jù)表結(jié)構(gòu)設(shè)計與遷移 (database migration)
1、數(shù)據(jù)庫遷移文件(數(shù)據(jù)表藍(lán)圖文件)
2校翔、創(chuàng)建相應(yīng)的藍(lán)圖文件弟跑,通過命令就可以創(chuàng)建相應(yīng)的數(shù)據(jù)表
3、對藍(lán)圖的修改防症,再通過命令就可以相應(yīng)更新數(shù)據(jù)表
相當(dāng)于擁有版本控制的動能
php artisan 查看artisan 相關(guān)命令孟辑,在migrate 下是數(shù)據(jù)表相關(guān)命令
php artisan migrate:status 狀態(tài)
php atrisan migrate 創(chuàng)建數(shù)據(jù)表,根據(jù)數(shù)據(jù)藍(lán)圖創(chuàng)建
database 目錄下/ migrations 遷移文件
對 Project 和 Task 兩張藍(lán)圖進(jìn)行設(shè)計 (設(shè)計文件)
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'); // 任務(wù)完成狀態(tài)
$table->timestamps();
});
使用的命令
php artisan migrate:rollback // 回滾 回到之前命令 前的狀態(tài)
php artisan migrate