ホームページ  >  記事  >  バックエンド開発  >  Laravelフレームワーク学習メモ(2) プロジェクトの実モデル(Models)、laravelmodels_PHPチュートリアル

Laravelフレームワーク学習メモ(2) プロジェクトの実モデル(Models)、laravelmodels_PHPチュートリアル

PHP中文网
PHP中文网オリジナル
2016-07-13 10:17:141245ブラウズ

前回の記事では開発環境の構築について紹介しましたが、この記事では実際のプロジェクト開発に焦点を当て、laravelフレームワークを段階的に理解していきます。まず、laravelフレームワークのモデルを理解しましょう (モデル)

mvc プロジェクトを開発する場合、モデルは最初のステップです。

モデリングから始めましょう。

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' => '',
 ),

、laravel ディレクトリにある php コマンド ツールである Artisan ツールを使用する必要があります

最初に移行を作成する必要がありますこれは asp.net mvc とほぼ同じです

laravel ディレクトリで、shfit を押しながら右クリックしてコマンド ウィンドウを開き、「artisan merge: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://www.php.cn/

<?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(&#39;posts&#39;, function(Blueprint $table) {
   $table->increments(&#39;id&#39;);
   $table->unsignedInteger(&#39;user_id&#39;);
   $table->string(&#39;title&#39;);
   $table->string(&#39;read_more&#39;);
   $table->text(&#39;content&#39;);
   $table->unsignedInteger(&#39;comment_count&#39;);
   $table->timestamps();
  });

  Schema::create(&#39;comments&#39;, function(Blueprint $table) {
   $table->increments(&#39;id&#39;);
   $table->unsignedInteger(&#39;post_id&#39;);
   $table->string(&#39;commenter&#39;);
   $table->string(&#39;email&#39;);
   $table->text(&#39;comment&#39;);
   $table->boolean(&#39;approved&#39;);
   $table->timestamps();
  });

   Schema::table(&#39;users&#39;, function (Blueprint $table) {
   $table->create();
   $table->increments(&#39;id&#39;);
   $table->string(&#39;username&#39;);
   $table->string(&#39;password&#39;);
   $table->string(&#39;email&#39;);
   $table->string(&#39;remember_token&#39;, 100)->nullable();
   $table->timestamps();
  });
 }

 /**
  * Reverse the migrations.
  *
  * @return void
  */
 public function down()
 {
  Schema::drop(&#39;posts&#39;);

  Schema::drop(&#39;comments&#39;);

  Schema::drop(&#39;users&#39;);
 }

}

を参照して、上記のコマンド ウィンドウに php 職人 移行 と入力し続けて、移行を実行します。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。