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

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

下面就从建模开始。

1.实体关系图,

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

下面开始laravel编码,编码之前首先得配置数据库连接,在app/config/database.php文件

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文件下生成一个带时间戳前缀的迁移文件

代码:

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 迁移经验的基本上发现这是出奇的相似啊。

接下来就是创建我们的实体结构,laravel 的结构生成器可以参考http://v4.golaravel.com/docs/4.1/schema

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

先写到这里明天继续

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


如何利用Laravel实现数据缓存功能
Laravel权限功能的实战应用:如何实现用户组织架构权限控制
如何在Laravel中实现基于权限的多语言支持
掌握Laravel控制台命令,利用参数传递的力量
如何利用Laravel实现数据分页和搜索功能
如何在Laravel中使用中间件进行日志记录
如何在Laravel中使用中间件进行数据迁移
如何在Laravel中使用中间件进行数据统计
如何利用Laravel实现邮件发送和接收功能
如何在Laravel中使用中间件进行数据导出
Laravel权限功能详解:如何定义和管理用户角色
如何在Laravel中使用中间件进行用户反馈
如何使用Laravel开发一个基于微信公众号的在线点餐系统
Laravel权限功能的可靠性保证:如何实现权限的冗余备份和恢复
Laravel权限功能的进阶应用:如何实现权限的可视化管理和配置
如何在Laravel中使用中间件进行数据加密传输
Laravel权限功能的最佳实践:如何实现权限缓存和性能优化
如何在Laravel中使用中间件进行API认证
如何在Laravel中使用中间件进行数据加速
如何利用Laravel实现数据验证和过滤功能