Laravel framework study notes (2) Project actual models (Models), laravelmodels_PHP tutorial

PHP中文网
Release: 2016-07-13 10:17:14
Original
1254 people have browsed it

The previous article has introduced the establishment of the development environment. This article will focus on actual project development and understand the laravel framework step by step. First, let’s understand the model of laravel framework (Models)

When developing an mvc project, models are the first step.

Let’s start with modeling.

1. Entity relationship diagram,

Since I don’t know any good modeling tools in PHP, here I use vs ado.net entity model data modeling

Let’s start laravel coding. Before coding, you must first configure the database connection. In the app/config/database.php file

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

After configuration, you need to use the artisan tool, which is a php command tool in the laravel directory

First you need to create a migration migrate through artisan, which is almost exactly the same as asp.net mvc

Shfit + right-click in the laravel directory to open the command window and enter artisan migrate:make create_XXXX to generate a migration file with a timestamp prefix under the app/database/migrations file

Code:

<?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()
 {
 
 }
 
}
Copy after login

Anyone who has experience in entityframework migration here basically finds that it is surprisingly similar.

The next step is to create our entity structure. Laravel’s structure generator can refer to //m.sbmmt.com/

<?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;);
 }

}
Copy after login

Continue to enter php artisan migrate in the above command window and the migration will be executed

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!