• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 4年前 (2022-01-25) 22次浏览 已收录 0个评论

Laravel

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

下面就从建模开始。

1.实体关系图,

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

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

代码:

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

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

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();  }); }<i>本文@来#源gaodai$ma#com搞$$代**码网</i><strong>搞代gaodaima码</strong> /**  * 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

先写到这里明天继续


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Laravel框架学习笔记(二)项目实战之模型(Models)_PHP

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址