Laravel5.1 database connections, creating databases, creating model, and creating controllers _php instances

Source: Internet
Author: User
Tags php programming phpmyadmin smarty template

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

Foreword: Laravel Creates a database, can actually create manually, such as old phpMyAdmin etc. all can.

One, the database connection:

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

Open File:

Found it:

db_host=127.0.0.1//Connection address does not use localhost
db_database=homestead//database name (need to be created beforehand)
db_username=root//Login name
db_ password=//password

Here I have changed into my local environment.

Second, the data table creation

CMD Create:

Cut to Laravel 5.1 's storage directory (project directory)

And 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 are errors about the database at this time, please check that the database connection is correct, I just planted a morning here (I phpmyadmin was modified by me, password casually input, but the original is empty, so that the connection to the database when random input password will 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-increasing
   $table->string (' title '); 
$table->text (' intro '); 
$table->text (' content ');
$table->timestamp (' published_at ');
 $table->timestamps (); Two fields automatically created: Created_at and Updated_at});


Then execute:

PHP Artisan Migrate

The table will be created automatically

Third, create modal

Perform:

PHP Artisan Make:model Article

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

Four, controller

I created this here manually. (It feels like crap, personal feeling phpmyadmin or navicat can create a database), in D:\laravel-v5.1.11\app\Http\Controllers\Articles The following created the artilcescontroller.php controller (I am using the Controller method below the sub file, see the previous article for specific actions).

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 the site topics: "Laravel Framework Introduction and Advanced Course", "PHP Excellent Development Framework Summary", "Smarty Template Primer Tutorial", "PHP date and Time usage summary", "PHP object-oriented Program Design Introductory Course ", PHP string (String) Usage summary," PHP+MYSQL Database operation Introduction Tutorial "and" PHP common database Operation Skills Summary "

I hope this article will help you with your PHP programming based on the Laravel framework.

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.