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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Laravel - Artisan Commands Laravel - Artisan Commands Aug 27, 2024 am 10:51 AM

Laravel - Artisan Commands - Laravel 5.7 comes with new way of treating and testing new commands. It includes a new feature of testing artisan commands and the demonstration is mentioned below ?

Comparison of the latest versions of Laravel and CodeIgniter Comparison of the latest versions of Laravel and CodeIgniter Jun 05, 2024 pm 05:29 PM

The latest versions of Laravel 9 and CodeIgniter 4 provide updated features and improvements. Laravel9 adopts MVC architecture and provides functions such as database migration, authentication and template engine. CodeIgniter4 uses HMVC architecture to provide routing, ORM and caching. In terms of performance, Laravel9's service provider-based design pattern and CodeIgniter4's lightweight framework give it excellent performance. In practical applications, Laravel9 is suitable for complex projects that require flexibility and powerful functions, while CodeIgniter4 is suitable for rapid development and small applications.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Laravel vs CodeIgniter: Which framework is better for large projects? Laravel vs CodeIgniter: Which framework is better for large projects? Jun 04, 2024 am 09:09 AM

When choosing a framework for large projects, Laravel and CodeIgniter each have their own advantages. Laravel is designed for enterprise-level applications, offering modular design, dependency injection, and a powerful feature set. CodeIgniter is a lightweight framework more suitable for small to medium-sized projects, emphasizing speed and ease of use. For large projects with complex requirements and a large number of users, Laravel's power and scalability are more suitable. For simple projects or situations with limited resources, CodeIgniter's lightweight and rapid development capabilities are more ideal.

Which is the better template engine, Laravel or CodeIgniter? Which is the better template engine, Laravel or CodeIgniter? Jun 03, 2024 am 11:30 AM

Comparing Laravel's Blade and CodeIgniter's Twig template engine, choose based on project needs and personal preferences: Blade is based on MVC syntax, which encourages good code organization and template inheritance. Twig is a third-party library that provides flexible syntax, powerful filters, extended support, and security sandboxing.

How does Go WebSocket integrate with databases? How does Go WebSocket integrate with databases? Jun 05, 2024 pm 03:18 PM

How to integrate GoWebSocket with a database: Set up a database connection: Use the database/sql package to connect to the database. Store WebSocket messages to the database: Use the INSERT statement to insert the message into the database. Retrieve WebSocket messages from the database: Use the SELECT statement to retrieve messages from the database.

Which one is more beginner-friendly, Laravel or CodeIgniter? Which one is more beginner-friendly, Laravel or CodeIgniter? Jun 05, 2024 pm 07:50 PM

For beginners, CodeIgniter has a gentler learning curve and fewer features, but covers basic needs. Laravel offers a wider feature set but has a slightly steeper learning curve. In terms of performance, both Laravel and CodeIgniter perform well. Laravel has more extensive documentation and active community support, while CodeIgniter is simpler, lightweight, and has strong security features. In the practical case of building a blogging application, Laravel's EloquentORM simplifies data manipulation, while CodeIgniter requires more manual configuration.

See all articles