Laravel framework learning notes (2) model of Project Practice (Models), laravelmodels

Source: Internet
Author: User
Tags what php

Laravel framework learning notes (2) model of Project Practice (Models), laravelmodels

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

Next we will start with modeling.

1. Object relationship diagram,

I don't know what php has to do with modeling tools. Here I use vs ado.net object model data modeling.

The following code starts laravel. Before encoding, you must 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' => '', ),

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

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

In the laravel directory, right-click shfit + and enter artisan migrate: make create_XXXX in the open command window. A migration file with a timestamp prefix is generated in 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() {  } }

The entityframework migration experience is surprisingly similar.

The next step is to create our entity structure. laravel's schema builder can refer to http://v4.golaravel.com/docs/4.1/schema

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

Enter php artisan migrate in the preceding command window to execute migration.

More about migration: http://v4.golaravel.com/docs/4.1/migrations

Write it here. Continue tomorrow.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.