Home Backend Development PHP Tutorial Laravel framework study notes (2) Project actual models (Models), laravelmodels_PHP tutorial

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

Jul 13, 2016 am 10:17 AM
laravel framework Model

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

}
Copy after login

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

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The world's most powerful open source MoE model is here, with Chinese capabilities comparable to GPT-4, and the price is only nearly one percent of GPT-4-Turbo The world's most powerful open source MoE model is here, with Chinese capabilities comparable to GPT-4, and the price is only nearly one percent of GPT-4-Turbo May 07, 2024 pm 04:13 PM

The world's most powerful open source MoE model is here, with Chinese capabilities comparable to GPT-4, and the price is only nearly one percent of GPT-4-Turbo

KAN, which replaces MLP, has been extended to convolution by open source projects KAN, which replaces MLP, has been extended to convolution by open source projects Jun 01, 2024 pm 10:03 PM

KAN, which replaces MLP, has been extended to convolution by open source projects

Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Apr 01, 2024 pm 07:46 PM

Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training

AI subverts mathematical research! Fields Medal winner and Chinese-American mathematician led 11 top-ranked papers | Liked by Terence Tao AI subverts mathematical research! Fields Medal winner and Chinese-American mathematician led 11 top-ranked papers | Liked by Terence Tao Apr 09, 2024 am 11:52 AM

AI subverts mathematical research! Fields Medal winner and Chinese-American mathematician led 11 top-ranked papers | Liked by Terence Tao

Hello, electric Atlas! Boston Dynamics robot comes back to life, 180-degree weird moves scare Musk Hello, electric Atlas! Boston Dynamics robot comes back to life, 180-degree weird moves scare Musk Apr 18, 2024 pm 07:58 PM

Hello, electric Atlas! Boston Dynamics robot comes back to life, 180-degree weird moves scare Musk

Time Series Forecasting NLP Large Model New Work: Automatically Generate Implicit Prompts for Time Series Forecasting Time Series Forecasting NLP Large Model New Work: Automatically Generate Implicit Prompts for Time Series Forecasting Mar 18, 2024 am 09:20 AM

Time Series Forecasting NLP Large Model New Work: Automatically Generate Implicit Prompts for Time Series Forecasting

FisheyeDetNet: the first target detection algorithm based on fisheye camera FisheyeDetNet: the first target detection algorithm based on fisheye camera Apr 26, 2024 am 11:37 AM

FisheyeDetNet: the first target detection algorithm based on fisheye camera

Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! May 06, 2024 pm 04:13 PM

Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year!

See all articles