Table of Contents
Laravel5.1 database connection, create database, create model and create controller method, laravel5.1model
Articles you may be interested in:
Home Backend Development PHP Tutorial Laravel5.1 database connection, database creation, model creation and controller methods, laravel5.1model_PHP tutorial

Laravel5.1 database connection, database creation, model creation and controller methods, laravel5.1model_PHP tutorial

Jul 12, 2016 am 08:55 AM
laravel model controller database

Laravel5.1 database connection, create database, create model and create controller method, laravel5.1model

This article describes the Laravel5.1 database connection, create database, create model and methods to create controllers. Share it with everyone for your reference, the details are as follows:

Foreword: Laravel creates a database, which can actually be created manually, such as the ancient 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 this file into a .env file)

Open file:

Found:

DB_HOST=127.0.0.1 //连接地址不使用localhost
DB_DATABASE=homestead //数据库名称(需要预先创建)
DB_USERNAME=root //登录名
DB_PASSWORD= //密码

Copy after login

I have modified this to match my local environment.

2. Data table creation

cmd created:

Cut to the storage directory of laravel 5.1 (project directory)

Then run:

php artisan make:migration create_articles_table --create=articles

Copy after login

You will get the created file: D:laravel-v5.1.11databasemigrations

If there is an error about the database at this time, please check whether the database connection is correct. I was stuck here all morning (my phpmyadmin was modified by me, and the password was entered casually, but it turned out to be empty, so it was difficult to connect to the database. I can’t get in if I just enter the password, but phpmyadmin can)

Open the newly created file and add fields:

public function up() { 
  Schema::create('articles', function (Blueprint $table) { $table->increments('id');// 主键 自增
   $table->string('title'); 
$table->text('intro'); 
$table->text('content');
$table->timestamp('published_at');
 $table->timestamps(); // 自动创建的两个字段:created_at 和 updated_at });
}

Copy after login

Then execute:

php artisan migrate

Copy after login

The table will be automatically created

3. Create modal

Execution:

php artisan make:model Article

Copy after login

The Article.php file will be created under the app directory. I don’t know exactly how to use this model, I will update it later

4. Controller

I created it manually here. (It feels very nonsense. I personally feel that phpmyadmin or navicat can create a database). I created the ArtilcesController.php controller under D:laravel-v5.1.11appHttpControllersArticles (I use the controller method under the sub-file. See the previous article for specific operations. ).

Code:

namespace App\Http\Controllers\Articles;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Article;//这个必须有,引入model,不然无法获取数据库数据
class ArticlesController extends Controller{
  public function index(){
    // $articles = Article::with('category')->latest()->paginate(15);
    $articles = Article::all();//获取所有数据
    //print_r($articles);
     $name = array(
       0=>array(
         "name"=>"123"
       ),
     );
    return view('articles.index', compact('articles'));//映射
  }
}

Copy after login

Readers who are interested in more information about Laravel can check out the special topics on this site: "Introduction and Advanced Tutorial on Laravel Framework", "Summary of PHP Excellent Development Framework", "Basic Tutorial on Getting Started with Smarty Templates", "php Date and Time" Usage Summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone’s PHP program design based on the Laravel framework.

Articles you may be interested in:

  • Introduction to Laravel 5 framework learning routes, controllers and views
  • ThinkPHP, ZF2, Yaf, Laravel framework routing competition
  • Laravel 4 Basic Tutorial: Views, Namespaces, and Routing
  • Learn the road to Laravel with me
  • Laravel framework routing configuration summary and setup tips
  • Laravel Detailed explanation of usage examples of Trait
  • Laravel’s method of implementing automatic dependency injection of constructors
  • Making APP interface (API) based on laravel
  • Experience of learning PHP framework Laravel
  • Get the previous and next data in Laravel
  • Laravel routing setting and sub-routing setting example analysis

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1117080.htmlTechArticleLaravel5.1 database connection, creating database, creating model and creating controller method, laravel5.1model This article describes the examples Laravel5.1 database connection, create database, create mode...
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

How to get the return code when email sending fails in Laravel? How to get the return code when email sending fails in Laravel? Apr 01, 2025 pm 02:45 PM

Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

Laravel Eloquent ORM in Bangla partial model search) Laravel Eloquent ORM in Bangla partial model search) Apr 08, 2025 pm 02:06 PM

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Laravel's geospatial: Optimization of interactive maps and large amounts of data Laravel's geospatial: Optimization of interactive maps and large amounts of data Apr 08, 2025 pm 12:24 PM

Efficiently process 7 million records and create interactive maps with geospatial technology. This article explores how to efficiently process over 7 million records using Laravel and MySQL and convert them into interactive map visualizations. Initial challenge project requirements: Extract valuable insights using 7 million records in MySQL database. Many people first consider programming languages, but ignore the database itself: Can it meet the needs? Is data migration or structural adjustment required? Can MySQL withstand such a large data load? Preliminary analysis: Key filters and properties need to be identified. After analysis, it was found that only a few attributes were related to the solution. We verified the feasibility of the filter and set some restrictions to optimize the search. Map search based on city

Laravel database migration encounters duplicate class definition: How to resolve duplicate generation of migration files and class name conflicts? Laravel database migration encounters duplicate class definition: How to resolve duplicate generation of migration files and class name conflicts? Apr 01, 2025 pm 12:21 PM

A problem of duplicate class definition during Laravel database migration occurs. When using the Laravel framework for database migration, developers may encounter "classes have been used...

How to effectively check the validity of Redis connections in Laravel6 project? How to effectively check the validity of Redis connections in Laravel6 project? Apr 01, 2025 pm 02:00 PM

How to check the validity of Redis connections in Laravel6 projects is a common problem, especially when projects rely on Redis for business processing. The following is...

See all articles