Laravel5.1 a database connection, creating a database, creating a model, and creating a controller laravel5.1model_php tutorial

Source: Internet
Author: User
Tags php framework smarty template

Laravel5.1 A database connection, create a database, create a model, and create a controller, Laravel5.1model


This example describes the Laravel5.1 database connection, creating a database, creating a model, and creating a controller. Share to everyone for your reference, as follows:

Preface: Laravel Create a database, can actually be created manually, such as the old phpMyAdmin.

One, database connection:

There is a. env file under the root directory (laravel5.1), and if not, there is a. Env.example then modify the file to an. env file)

Open File:

Found it:

db_host=127.0.0.1//Connection address does not use Localhostdb_database=homestead//database name (requires pre-creation) Db_username=root//Login db_password=//password

I have changed my local environment here.

Second, data table creation

CMD creation:

Cut to Laravel 5.1 storage directory (project directory)

Then run:

PHP Artisan make:migration create_articles_table--create=articles

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

If there is an error about the database at this point, please detect the database connection is correct, I was here a morning (I phpmyadmin was modified by me, password random input, but the original is empty, so that the connection database when you enter the password can not go in, But phpMyAdmin can)

Open the newly created file, and then add the fields:

Public function up () {   schema::create (' articles ', function (Blueprint $table) {$table->increments (' id ');//primary key self-increment   $table->string (' title '), $table->text (' intro '), $table->text (' content '); $table->timestamp (' Published_at '); $table->timestamps (); Automatically created two fields: Created_at and Updated_at});}

Then execute:

PHP Artisan Migrate

The table is created automatically

Iii. creation of modal

Perform:

PHP Artisan Make:model Article

The article.php file is created under the app directory. Specifically how to use this model, temporarily not clear later to fill

Four, the controller

I created it manually here. (Feeling very nonsense, personal feeling phpmyadmin or navicat can create a database), in D:\laravel-v5.1.11\app\Http\Controllers\Articles The artilcescontroller.php controller is created below (I use the Controller method under sub-files, as described in 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 have, introduce model, otherwise cannot get database data class Articlescontroller extends controller{public  function index () {    //$articles = Article::with (' category ')->latest ()->paginate ();    $articles = Article::all ();//Get All Data    //print_r ($articles);     $name = Array (       0=>array (         "name" = "123"       ),     );    Return view (' Articles.index ', compact (' articles '));//Map  }}

More interested in laravel related content readers can view this site topic: "Laravel Framework Introductory and Advanced tutorial", "PHP Excellent Development Framework Summary", "Smarty Template Primer Basic Tutorial", "PHP date and Time usage summary", "PHP object-oriented Programming introduction tutorial "," PHP String Usage Summary "," Introduction to Php+mysql Database Operations "and" PHP common database Operation Skills Summary "

It is hoped that this article is helpful to the PHP program design based on Laravel framework.

Articles you may be interested in:

    • Introduction to routing, controllers, and views of Laravel 5 Framework Learning
    • thinkphp, ZF2, YAF, laravel framework routing big battle
    • Laravel 4 View, namespaces, routes for the beginner tutorial
    • Follow me on the route to Laravel.
    • Laravel Framework Routing configuration summary, Setup tips Daquan
    • A detailed example of trait usage in Laravel
    • Laravel method of implementing automatic dependency injection of constructors
    • Create an App interface (API) based on Laravel
    • PHP Framework Laravel Learning Experience
    • Get previous and next data in Laravel
    • Laravel Route setting and sub-route setting example analysis

http://www.bkjia.com/PHPjc/1117080.html www.bkjia.com true http://www.bkjia.com/PHPjc/1117080.html techarticle Laravel5.1 A database connection, create a database, create a model, and create a controller, Laravel5.1model This example describes Laravel5.1 database connection, creating a database, creating a mode ...

  • Related Article

    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.