温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

编写Laravel框架项目实战之模型的方法教程

发布时间:2021-09-29 09:30:16 来源:亿速云 阅读:99 作者:iii 栏目:开发技术

本篇内容主要讲解“编写Laravel框架项目实战之模型的方法教程”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“编写Laravel框架项目实战之模型的方法教程”吧!

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

下面就从建模开始。

1.实体关系图,

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

编写Laravel框架项目实战之模型的方法教程

下面开始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

<?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 将执行迁移

到此,相信大家对“编写Laravel框架项目实战之模型的方法教程”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI