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.