Laravel5.1 database connection, database creation, model creation, and controller creation methods, laravel5.1model _ PHP Tutorial

Source: Internet
Author: User
Laravel5.1 database connection, database creation, model creation, and controller creation methods, laravel5.1model. Laravel5.1 database connection, database creation, model creation, and controller creation methods, laravel5.1model this article describes Laravel5.1 database connection, database creation, mode Laravel5.1 database connection, database creation, model creation, and controller creation methods. laravel5.1model

This example describes how to connect to a Laravel5.1 database, create a database, create a model, and create a controller. We will share this with you for your reference. The details are as follows:

Laravel can create databases manually, for example, phpmyadmin.

1. database connection:

In the root directory (there is a. env file under laravel5.1. if not, there will be a. env. example and then modify the file to a. env file)

Open the file:

Find:

DB_HOST = 127.0.0.1 // The connection address does not use localhostDB_DATABASE = homestead // database name (which must be created in advance) DB_USERNAME = root // login name DB_PASSWORD = // password

Here I have changed it to my local environment.

II. create a data table

Cmd creation:

Switch to the storage directory (Project Directory) of laravel 5.1)

Then run:

php artisan make:migration create_articles_table --create=articles

Will get the created File: D: \ laravel-v5.1.11 \ database \ migrations

If a database error occurs at this time, check whether the database connection is correct. I planted it here for one morning (I modified phpmyadmin and entered the password casually, however, it turns out to be empty, so that you cannot enter the password when you connect to the database, but phpmyadmin does)

Open the newly created file and add the following fields:

Public function up () {Schema: create ('articles', function (Blueprint $ table) {$ table-> increments ('id '); // primary key auto-increment $ table-> string ('title'); $ table-> text ('Intro'); $ table-> text ('content '); $ table-> timestamp ('hhed _ at'); $ table-> timestamps (); // Two automatically created fields: created_at and updated_at });}

Then execute:

php artisan migrate

The table is automatically created.

3. create modal

Run:

php artisan make:model Article

The Article. php file is created under the app directory. How to use this model?

IV. Controller

Manually created here. (Feeling very nonsense, personal feeling phpmyadmin or navicat can create a database), in D: \ laravel-v5.1.11 \ app \ Http \ Controllers \ Articles created ArtilcesController. php controller (I am using the controller method below the sub-file. for specific operations, see the previous article ).

Code:

Namespace App \ Http \ Controllers \ Articles; use Illuminate \ Http \ Request; use App \ Http \ Requests; use App \ Http \ Controllers \ Controller; use App \ Article; // This must be available and model is introduced. Otherwise, the database data class ArticlesController extends Controller {public function index () {// $ articles = Article: with ('Category ') cannot be obtained ') -> latest ()-> paginate (15); $ articles = Article: all (); // Obtain all data // print_r ($ articles ); $ name = array (0 => array ("name" => "123"),); return view ('Articles. index', compact ('Articles '); // ing }}

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.