Laravel框架学习笔记(二)项目实战之模型(Models)

   2015-09-03 0
核心提示:上一篇已经介绍开发环境的搭建,这篇将从项目实战开发,一步一步了解laravel框架。首先我们来了解下laravel框架的模型 (Models)

在开发mvc项目时,models都是第一步。

下面就从建模开始。

1.实体关系图,

由于不知道php有什么好的建模工具,这里我用的vs ado.net实体模型数据建模

Laravel框架学习笔记(二)项目实战之模型(Models)

下面开始laravel编码,编码之前首先得配置数据库连接,在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文件下生成一个带时间戳前缀的迁移文件

代码:

<" alt="Laravel框架学习笔记(二)项目实战之模型(Models)" />
<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');
 }

}

继续在上面的命令窗口输入php artisan migrate 将执行迁移

更多迁移相关知识:http://v4.golaravel.com/docs/4.1/migrations

先写到这里明天继续

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

点击排行