Laravel框架學習筆記(二)項目實戰(zhàn)之模型(Models)
在開發(fā)mvc項目時,models都是第一步。
下面就從建模開始。
1.實體關系圖,
由于不知道php有什么好的建模工具,這里我用的vs ado.net實體模型數(shù)據(jù)建模

下面開始laravel編碼,編碼之前首先得配置數(shù)據(jù)庫連接,在app/config/database.php文件
'mysql' => array( 'driver' => 'mysql', 'read' => array( 'host' => '127.0.0.1:3306', ), 'write' => array( 'host' => '127.0.0.1:3306' ), 'database' => 'test', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ),
配置好之后,需要用到artisan工具,這是一個php命令工具在laravel目錄中
首先需要要通過artisan建立一個遷移 migrate ,這點和asp.net mvc幾乎是一模一樣
在laravel目錄中 shfit+右鍵打開命令窗口 輸入artisan migrate:make create_XXXX會在app/database/migrations文件下生成一個帶時間戳前綴的遷移文件
代碼:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTablenameTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
看到這里有entityframework 遷移經驗的基本上發(fā)現(xiàn)這是出奇的相似啊。
接下來就是創(chuàng)建我們的實體結構,laravel 的結構生成器可以參考http://v4.golaravel.com/docs/4.1/schema
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTablenameTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function(Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->string('title');
$table->string('read_more');
$table->text('content');
$table->unsignedInteger('comment_count');
$table->timestamps();
});
Schema::create('comments', function(Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('post_id');
$table->string('commenter');
$table->string('email');
$table->text('comment');
$table->boolean('approved');
$table->timestamps();
});
Schema::table('users', function (Blueprint $table) {
$table->create();
$table->increments('id');
$table->string('username');
$table->string('password');
$table->string('email');
$table->string('remember_token', 100)->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('posts');
Schema::drop('comments');
Schema::drop('users');
}
}
繼續(xù)在上面的命令窗口輸入php artisan migrate 將執(zhí)行遷移
更多遷移相關知識:http://v4.golaravel.com/docs/4.1/migrations
先寫到這里明天繼續(xù)
- Laravel框架源碼解析之入口文件原理分析
- Laravel框架源碼解析之反射的使用詳解
- Laravel源碼解析之路由的使用和示例詳解
- 通過源碼解析Laravel的依賴注入
- laravel model模型定義實現(xiàn)開啟自動管理時間created_at,updated_at
- laravel model模型處理之修改查詢或修改字段時的類型格式案例
- Laravel5.1 框架模型工廠ModelFactory用法實例分析
- Laravel 5框架學習之模型、控制器、視圖基礎流程
- Laravel模型事件的實現(xiàn)原理詳解
- Laravel模型間關系設置分表的方法示例
- laravel學習教程之關聯(lián)模型
- Laravel框架源碼解析之模型Model原理與用法解析
相關文章
Zend Framework實現(xiàn)多服務器共享SESSION數(shù)據(jù)的方法
這篇文章主要介紹了Zend Framework實現(xiàn)多服務器共享SESSION數(shù)據(jù)的方法,詳細分析了SESSION數(shù)據(jù)共享的原理與實現(xiàn)技巧,需要的朋友可以參考下2016-03-03
yii2中使用webuploader實現(xiàn)圖片上傳的實戰(zhàn)項目
本篇文章主要主要介紹了yii2中使用webuploader實現(xiàn)圖片上傳的實戰(zhàn)項目,具有一定的參考價值,有興趣的同學可以了解一下2017-09-09
codeigniter中view通過循環(huán)顯示數(shù)組數(shù)據(jù)的方法
這篇文章主要介紹了codeigniter中view通過循環(huán)顯示數(shù)組數(shù)據(jù)的方法,實例分析了codeigniter中view方法與數(shù)組遍歷的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03

