Table of Contents
Laravel5.1数据库连接、创建数据库、创建model及创建控制器的方法,laravel5.1model
您可能感兴趣的文章:
Home php教程 php手册 Laravel5.1数据库连接、创建数据库、创建model及创建控制器的方法,laravel5.1model

Laravel5.1数据库连接、创建数据库、创建model及创建控制器的方法,laravel5.1model

Jun 13, 2016 am 08:42 AM
laravel model controller database

Laravel5.1数据库连接、创建数据库、创建model及创建控制器的方法,laravel5.1model

本文实例讲述了Laravel5.1数据库连接、创建数据库、创建model及创建控制器的方法。分享给大家供大家参考,具体如下:

前言:laravel创建数据库,实际可以手动创建,如古老的phpmyadmin 等均可以。

一、数据库连接:

在根目录(laravel5.1下面有个.env文件,如果没有则会有个.env.example然后将此文件修改成.env文件即可)

打开文件:

找到:

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

Copy after login

这里我已经修改成我本地环境的了。

二、数据表创建

cmd创建:

切到laravel 5.1的存放目录(项目目录)

然后运行:

php artisan make:migration create_articles_table --create=articles

Copy after login

会得到创建的文件:D:\laravel-v5.1.11\database\migrations

如果此时出现关于数据库的错误,请检测数据库连接是否正确,我就是在这里栽了一上午(我phpmyadmin被我给修改了,密码随便输入,但是原来是空的,以至于连接数据库的时候随便输入密码就进不去,但是phpmyadmin却可以)

打开新创建的文件,然后增加字段:

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

然后执行:

php artisan migrate

Copy after login

就会自动创建表

三、创建modal

执行:

php artisan make:model Article

Copy after login

会在app目录下面创建Article.php文件。具体怎么使用这个model,暂时不清楚后期再来补

四、控制器

我这里手动创建的。(感觉很扯淡,个人感觉phpmyadmin或者navicat可以创建数据库),在D:\laravel-v5.1.11\app\Http\Controllers\Articles 下面创建了ArtilcesController.php控制器(我使用的是子文件下面的控制器方式,具体操作见前面文章)。

代码:

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

更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

您可能感兴趣的文章:

  • Laravel 5框架学习之路由、控制器和视图简介
  • ThinkPHP、ZF2、Yaf、Laravel框架路由大比拼
  • Laravel 4 初级教程之视图、命名空间、路由
  • 跟我学Laravel之路由
  • Laravel框架路由配置总结、设置技巧大全
  • Laravel中Trait的用法实例详解
  • Laravel实现构造函数自动依赖注入的方法
  • 基于laravel制作APP接口(API)
  • PHP框架Laravel学习心得体会
  • Laravel 中获取上一篇和下一篇数据
  • Laravel路由设定和子路由设定实例分析
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)

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.

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.

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->

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 and the Backend: Powering Web Application Logic Laravel and the Backend: Powering Web Application Logic Apr 11, 2025 am 11:29 AM

How does Laravel play a role in backend logic? It simplifies and enhances backend development through routing systems, EloquentORM, authentication and authorization, event and listeners, and performance optimization. 1. The routing system allows the definition of URL structure and request processing logic. 2.EloquentORM simplifies database interaction. 3. The authentication and authorization system is convenient for user management. 4. The event and listener implement loosely coupled code structure. 5. Performance optimization improves application efficiency through caching and queueing.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Apr 18, 2025 am 09:24 AM

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

See all articles