New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2016 版 Laravel 系列入门教程(一)【最适合中国人的 Laravel 教程】 #4

Closed
johnlui opened this Issue Jun 6, 2016 · 75 comments

Comments

Projects
None yet
@johnlui
Owner

johnlui commented Jun 6, 2016

基于 Laravel 5.5 的 2017 版教程已经发布:#16

本教程示例代码见:https://github.com/johnlui/Learn-Laravel-5

在任何地方卡住,最快的办法就是去看示例代码。

本文基于 Laravel 5.2 版本,无奈 5.2 的中文文档还没有跟上,大家勉强看一下 5.1 的吧:

Laravel 5.1 中文文档:
  1. http://laravel-china.org/docs/5.1
  2. http://www.golaravel.com/laravel/docs/5.1/

默认条件

你应该懂得 PHP 网站运行的基础知识,并且有了一个完善的开发环境。跟随本教程走完一遍,你将会得到一个基础的包含登录、后台编辑、前台评论的简单 blog 系统。

Tips

  1. 环境要求:PHP 5.5.9+,MySQL 5.1+
  2. 本教程不推荐完全不懂 PHP 与 MVC 编程的人学习,Laravel 的学习曲线不仅仅是陡峭,而且耗时很长,请先做好心理准备。
  3. 这不是 “一步一步跟我做” 教程。本教程需要你付出一定的心智去解决一些或大或小的隐藏任务,以达到真正理解 Laravel 运行逻辑的目的。
  4. 本宝宝使用 Safari 截图是为了好看,宝宝们在开发时请选择 Chrome 哦~

开始学习

1. 安装

许多人被拦在了学习 Laravel 的第一步:安装。并不是因为安装有多复杂,而是因为【众所周知的原因】。在此我推荐一个 composer 全量中国镜像:http://pkg.phpcomposer.com/ 。启用 Composer 镜像服务作为本教程的第一项小作业请自行完成哦。

镜像配置完成后,在终端(Terminal 或 CMD)里切换到你想要放置该网站的目录下(如 C:\wwwroot、/Library/WebServer/Documents/、/var/www/html、/etc/nginx/html 等),运行命令:

composer create-project laravel/laravel learnlaravel5 5.2.31

然后,稍等片刻,当前目录下就会出现一个叫 learnlaravel5 的文件夹,安装完成啦~

2. 运行

为了尽可能地减缓学习曲线,推荐宝宝们使用 PHP 内置 web 服务器驱动我们的网站。运行以下命令:

cd learnlaravel5/public
php -S 0.0.0.0:1024

这时候访问 http://127.0.0.1:1024 就是这个样子的:

我在本地 hosts 中绑定了 fuck.io 到 127.0.0.1,所以截图中我的域名是 fuck.io 而不是 127.0.0.1,其实他们是完全等价的。

这时候你可能要问了:为什么本宝宝的页面是一片空白?请使用开发者工具查看网络请求,只要是 200 状态就说明运行成功了,空白是因为这个页面引用了 Google Fonts,你懂的~

至于为什么选择 1024 端口?因为他是 *UNIX 系统动态端口的开始,是我们不需要 root 权限就可以随意监听的数值最小的端口。

另外,建议不熟悉 PHP 运行环境搭建的宝宝们不要轻易尝试使用 Apache 或 Nginx 驱动 Laravel,特别是在开启了 SELinux 的 Linux 系统上跑。关于 Laravel 在 Linux 上部署的大坑,本宝宝可能要单写一篇长文分享给宝宝们。

3. 体验牛逼闪闪的 Auth 系统

Laravel 利用 PHP5.4 的新特性 trait 内置了非常完善好用的简单用户登录注册功能,适合一些不需要复杂用户权限管理的系统,例如公司内部用的简单管理系统。

激活这个功能非常容易,运行以下命令:

php artisan make:auth

访问 http://fuck.io:1024/login,如果你本地已经科学上网,那就能看到以下页面:

如果你不能科学上网,就把 learnlaravel5/resources/views/layouts/app.blade.php 中的 https://fonts.googleapis.com 换成 http://fonts.useso.com 就好啦~

cdnjs.cloudflare.com 可能会短暂抽风,不要灰心,那只是大墙在抖动,一会儿就好了。

4. 连接数据库

接下来我们要连接数据库了,请自行准备好 MySQL 服务哦。

a. 修改配置

不出意外的话,learnlaravel5 目录下已经有了一个 .env 文件,如果没有,可以复制一份 .env.example 文件重命名成 .env,修改下面几行的值:

DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel5
DB_USERNAME=root
DB_PASSWORD=password

推荐新建一个名为 laravel5 的数据库,并且使用 root 账户直接操作,降低学习数据库的成本。

数据库配置好之后,在登录界面填写任意邮箱和密码,点击 Login,你应该会得到以下画面:

它说 users 表不存在呀,接下来我们将见识 Laravel 另外一个实用特性。

b. 进行数据库迁移(migration)

运行命令:

php artisan migrate

我们得到了如下结果:

数据库迁移成功!赶快打开 http://fuck.io:1024/home 注册一个用户试试吧~

下图是本宝宝注册了一个 username 为 1 用户:

c. migration 是啥?

打开 learnlaravel5/database/migrations/2014_10_12_000000_create_users_table.php 文件,你肯定能一眼看出它的作用:用 PHP 描述数据库构造,并且使用命令行一次性部署所有数据库结构。

5. 使用 Laravel 的“葵花宝典”:Eloquent

Eloquent 是 Laravel 的 ORM,是 Laravel 系统中最强大的地方,没有之一。当初 Laravel 作者在开发第一版的时候花了整整三分之一的时间才搞出来 Eloquent。当然,“欲练此功,必先自宫”,Eloquent 也是 Laravel 中最慢的地方,迄今无法解决。(路由、自动载入、配置分散、视图引发的性能问题都通过缓存几乎彻底解决了)

当然,我们还是要承袭第一版教程中对 Eloquent ORM 的描述:鹅妹子英!

如果你想深入地了解 Eloquent,可以阅读系列文章:深入理解 Laravel Eloquent(一)——基本概念及用法

a. Eloquent 是什么

Eloquent 是 Laravel 内置的 ORM 系统,我们的 Model 类将继承自 Eloquent 提供的 Model 类,然后,就天生具备了数十个异常强大的函数,从此想干啥事儿都是一行代码就搞定。

b. 怎么用?

我们使用 Artisan 工具新建 Model 类及其附属的 Migration 和 Seeder(数据填充)类。

运行以下命令:

php artisan make:model Article

去看看你的 app 目录,下面是不是多了一个 Article.php 文件?那就是 Artisan 帮我们生成的 Model 文件:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    //
}

如此简洁有力的代码,隐藏了背后极高的难度和巨大的复杂度,让我们闭上眼睛,静静地感受 Laravel 的优雅吧 (~ ̄▽ ̄)~

下面是几个简单的例子:

找到 id 为 2 的文章打印其标题

$article = Article::find(2);

echo $article->title;

查找标题为“我是标题”的文章,并打印 id

$article = Article::where('title', '我是标题')->first();

echo $article->id;

查询出所有文章并循环打印出所有标题

$articles = Article::all(); // 此处得到的 $articles 是一个对象集合,可以在后面加上 '->toArray()' 变成多维数组。

foreach ($articles as $article) {

    echo $article->title;

}

查找 id 在 10~20 之间的所有文章并打印所有标题

$articles = Article::where('id', '>', 10)->where('id', '<', 20)->get();

foreach ($articles as $article) {

    echo $article->title;

}

查询出所有文章并循环打印出所有标题,按照 updated_at 倒序排序

$articles = Article::where('id', '>', 10)->where('id', '<', 20)->orderBy('updated_at', 'desc')->get();

foreach ($articles as $article) {

    echo $article->title;

}

6. 使用 Migration 和 Seeder

接下来我们生成对应 Article 这个 Model 的 Migration 和 Seeder。

a. 使用 artisan 生成 Migration

在 learnlaravel5 目录下运行命令:

php artisan make:migration create_article_table

成功之后打开 learnlaravel5/database/migrations,你会发现有一个名为 2*****_create_article_table 的文件被创建了。我们修改他的 up 函数为:

public function up()
{
    Schema::create('articles', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('title');
        $table->text('body')->nullable();
        $table->integer('user_id');
        $table->timestamps();
    });
}

这几行代码描述的是 Article 对应的数据库中那张表的结构。Laravel 默认 Model 对应的表名是这个英文单词的复数形式,在这里,就是 articles。接下来让我们把 PHP 代码变成真实的 MySQL 中的数据表,运行命令:

php artisan migrate

执行成功后,articles 表已经出现在数据库里了:

上图中的软件叫 Sequel Pro,是一个开源的 MAC 下 MySQL GUI 管理工具,超好用,推荐给用 MAC 的同学。

b. 使用 artisan 生成 Seeder

Seeder 是我们接触到的一个新概念,字面意思为播种机。Seeder 解决的是我们在开发 web 应用的时候,需要手动向数据库中填入假数据的繁琐低效问题。

运行以下命令创建 Seeder 文件:

php artisan make:seeder ArticleSeeder

我们会发现 learnlaravel5/database/seeds 里多了一个文件 ArticleSeeder.php,修改此文件中的 run 函数为:

public function run()
{
    DB::table('articles')->delete();

    for ($i=0; $i < 10; $i++) {
        \App\Article::create([
            'title'   => 'Title '.$i,
            'body'    => 'Body '.$i,
            'user_id' => 1,
        ]);
    }
}

上面代码中的 \App\Article 为命名空间绝对引用。如果你对命名空间还不熟悉,可以读一下 《PHP 命名空间 解惑》,很容易理解的。

接下来我们把 ArticleSeeder 注册到系统内。修改 learnlaravel5/database/seeds/DatabaseSeeder.php 中的 run 函数为:

public function run()
{
    $this->call(ArticleSeeder::class);
}

由于 database 目录没有像 app 目录那样被 composer 注册为 psr-4 自动加载,采用的是 psr-0 classmap 方式,所以我们还需要运行以下命令把 ArticleSeeder.php 加入自动加载系统,避免找不到类的错误:

composer dump-autoload

然后执行 seed:

php artisan db:seed

你应该得到如下结果:

这时候刷新一下数据库中的 articles 表,会发现已经被插入了 10 行假数据:

下一步:2016 版 Laravel 系列入门教程(二)【最适合中国人的 Laravel 教程】

@cullenx

This comment has been minimized.

cullenx commented Jun 7, 2016

文中图示 :连接mysql的工具 是那个?谢谢

@solarhell

This comment has been minimized.

solarhell commented Jun 7, 2016

@Samtoto

This comment has been minimized.

Samtoto commented Jun 8, 2016

支持下!实在不想写 thinkphp 的代码了,可是要 TP 的公司好多。
希望 laravel 早日占领国内市场。

@johnlui

This comment has been minimized.

Owner

johnlui commented Jun 8, 2016

这是本地域名,本质上讲就是 127.0.0.1:1024/home

From My iPhone

在 2016年6月8日,17:55,卓文吉 notifications@github.com 写道:

http://fuck.io:1024/home 打不开的


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

@magicwater

This comment has been minimized.

magicwater commented Jun 12, 2016

http://127.0.0.1/learnlaravel5/public/login 提示Not Found

The requested URL /learnlaravel5/public/login was not found on this server.

@johnlui

This comment has been minimized.

Owner

johnlui commented Jun 12, 2016

@magicwater 伪静态问题。如果你搞不定这个问题,建议先使用 PHP 内置 HTTP 服务器来开发。

@magicwater

This comment has been minimized.

magicwater commented Jun 12, 2016

我先研究一下,感谢回复。

@yidashi

This comment has been minimized.

yidashi commented Jun 12, 2016

这教程写的太好了,我转走了啊

@CaijiOrz

This comment has been minimized.

CaijiOrz commented Jun 14, 2016

请问我在php artisan db:seed的时候找不到类是为什么?
这里是提示信息:Class ArticleSeeder does not exist
我下载了您的源码,是一样的...我也去翻阅了官方文档...同样的代码..就是找不到类
系统是Xubuntu 环境是PHP内置集成环境

@johnlui

This comment has been minimized.

Owner

johnlui commented Jun 14, 2016

@Lyttoni DatabaseSeeder.php 和 ArticleSeeder.php 内容都和示例代码一样?

@CaijiOrz

This comment has been minimized.

CaijiOrz commented Jun 14, 2016

@johnlui 是的,都是一样的.并且官方文档中的DatabaseSeeder.php中还多了一行use Illuminate\Database\Eloquent\Model;。。

@johnlui

This comment has been minimized.

Owner

johnlui commented Jun 15, 2016

@Lyttoni 那是因为文档是 5.1 的。我可能知道原因了,教程里面没写。因为 database 文件夹的自动加载是 classmap 的,就是说手动扫描,所以新建了这个类文件之后,还需要 composer dump-autoload 来将其加入自动加载。我会更新一下教程,感谢反馈~

@CaijiOrz

This comment has been minimized.

CaijiOrz commented Jun 15, 2016

@johnlui 十分感谢,问题已经解决了:D ,在composer dump-autoload的时候会写入数据到config.json...Linux下需要给予权限

@j0hnnie

This comment has been minimized.

j0hnnie commented Jun 23, 2016

ErrorException in 4efa357d8f6ebc1082e0198e3a75bc42ff5dc523.php line 12:
Undefined variable: errors (View: F:\xampp\htdocs\maishou.dev\resources\views\auth\register.blade.php)
in 4efa357d8f6ebc1082e0198e3a75bc42ff5dc523.php line 12
at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44
at PhpEngine->evaluatePath('F:\xampp\htdocs\maishou.dev\storage\framework\views/4efa357d8f6ebc1082e0198e3a75bc42ff5dc523.php', array('__env' => object(Factory), 'app' => object(Application))) in CompilerEngine.php line 59
at CompilerEngine->get('F:\xampp\htdocs\maishou.dev\resources\views/auth/register.blade.php', array('__env' => object(Factory), 'app' => object(Application))) in View.php line 149
at View->getContents() in View.php line 120
at View->renderContents() in View.php line 85
at View->render() in Response.php line 53
at Response->setContent(object(View)) in Response.php line 201
at Response->__construct(object(View)) in Router.php line 1085
at Router->prepareResponse(object(Request), object(View)) in ControllerDispatcher.php line 95
at ControllerDispatcher->Illuminate\Routing{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing{closure}(object(Request)) in RedirectIfAuthenticated.php line 24
at RedirectIfAuthenticated->handle(object(Request), object(Closure))
at call_user_func_array(array(object(RedirectIfAuthenticated), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(AuthController), object(Route), object(Request), 'showRegistrationForm') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\Auth\AuthController', 'showRegistrationForm') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 724
at Router->Illuminate\Routing{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing{closure}(object(Request)) in Debugbar.php line 49
at Debugbar->handle(object(Request), object(Closure))
at call_user_func_array(array(object(Debugbar), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54

按步骤,到“b. 进行数据库迁移(migration)”就进行不下去了。 。。老提示这个错误

@Smartyue

This comment has been minimized.

Smartyue commented Jun 30, 2016

运行 php artisan db:seed之后,会报错:
[Symfony\Component\Debug\Exception\FatalErrorException]
parse error, expecting `"function (T_FUNCTION)"'
这个怎么解决呢?

@johnlui

This comment has been minimized.

Owner

johnlui commented Jun 30, 2016

@Smartyue fatal error 是语法错误,目测 seeder 里面语法错了

@Smartyue

This comment has been minimized.

Smartyue commented Jun 30, 2016

@johnlui 额,刚刚看了下您的源码,我已经解决了,是因为我在app目录下的Article.php中写了东西,受您教程的影响,犯了低级错误....

@scplay

This comment has been minimized.

scplay commented Jul 11, 2016

一个前端进公司就让我去学Laravel,一头雾水,感觉中文的laravel教程真是少有好的,现在渐渐感觉 Laravel 的优雅了,然而我还是喜欢node :P

@David-Nangong

This comment has been minimized.

David-Nangong commented Jul 19, 2016

写的太好了,作者大大辛苦了,灰常感谢~~
请问能否转载您的文章并注明出处?
附一个我找到的5.2版本的中文文档,质量我不做评判,不过读英文文档有些地方实在搞不定还是可以参考一下的:
Laravel 5.2 中文文档

@Pine0302

This comment has been minimized.

Pine0302 commented Jul 19, 2016

你好,我看到你写的article 类名是单数,生成数据库的时候 Schema::create('articles', function... 用的复数,最后在用Seeder生成数据的时候没问题的,我试着把数据库生成单数的article,最后用seeder生成的时候说没找到复数的articles表,我想问下这个是规定吗,数据库里的表比类名多个s?

@johnlui

This comment has been minimized.

Owner

johnlui commented Jul 19, 2016

@johnlui

This comment has been minimized.

Owner

johnlui commented Jul 19, 2016

@Pine0302 是的,我教程里写了呦

@Pine0302

This comment has been minimized.

Pine0302 commented Jul 21, 2016

找到了

@Pine0302

This comment has been minimized.

Pine0302 commented Jul 21, 2016

您的文章写的真好,看起来很舒服,容易入门

@qbmiller

This comment has been minimized.

qbmiller commented Jul 24, 2016

请问 Article::find(2); 着小例子,在哪写测试啊,

@qbmiller

This comment has been minimized.

qbmiller commented Jul 24, 2016

➜  laravel php artisan db:seed

  [BadMethodCallException]
  Call to undefined method Illuminate\Database\Query\Builder::setContainer()

can't find setContainer()

@ucoker

This comment has been minimized.

ucoker commented Aug 10, 2016

b. 进行数据库迁移(migration) 这个步骤migration成功。
尝试在网页进行注册,出现错误

TokenMismatchException in VerifyCsrfToken.php line 67:

完全按教程来,请问我哪个地方有误?

tokenmismatchexception

@lz408682690

This comment has been minimized.

lz408682690 commented Aug 16, 2016

执行 :
cd learnlaravel5/public
php -S 0.0.0.0:1024

以后,浏览器访问:http://127.0.0.1:1024
提示 : 127.0.0.1 拒绝了我们的连接请求。求指导。

PS: http://localhosthttp://127.0.0.1 都能正常显示

@ucoker

This comment has been minimized.

ucoker commented Aug 16, 2016

@lz408682690

This comment has been minimized.

lz408682690 commented Aug 17, 2016

@ucoker 好的,我试下。谢谢!

@NominationP

This comment has been minimized.

NominationP commented Dec 21, 2016

php -S 0.0.0.0:1024
not to exit , or you can't open http://127.0.0.1:1024/

@zl02655931

This comment has been minimized.

zl02655931 commented Dec 27, 2016

我用的5.1 ,在db:seed创建填充数据的时候会提示表不存在,我用的article命名,而系统自动识别为articles,所以会报错。
然后我在模型里绑定了表名就正确了。不知道是不是版本问题。
不过老师讲的很好。希望能学到实战项目级别的教程

@zhmsong

This comment has been minimized.

zhmsong commented Jan 22, 2017

优秀作品!力透纸背!造福人类!

@benbenlang

This comment has been minimized.

benbenlang commented Feb 2, 2017

修改过2017_02_02_202126_create_article_table 后执行php artisan migrate报错SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' alre
ady exists

大哥你这个教程能更新一下吗,这么多星啊

@johnlui

This comment has been minimized.

Owner

johnlui commented Feb 3, 2017

@benbenlang 翻译一下“Table 'users' already exists”

@benbenlang

This comment has been minimized.

benbenlang commented Feb 3, 2017

@johnlui 我知道什么意思,但我不知道怎么解决,我重建了项目还是这样.我是代码新手,你能写个2017版的吗,这样新手会遇到的问题少一点,谢谢你!

@johnlui

This comment has been minimized.

Owner

johnlui commented Feb 3, 2017

@thefireb

This comment has been minimized.

thefireb commented Feb 24, 2017

你好
image这个是怎么回事啊,安装您的教程安装的

@sabakugaara

This comment has been minimized.

sabakugaara commented Feb 24, 2017

需要安装 php 的依赖 dom 扩展,参见安装

@yaoshiyizhu

This comment has been minimized.

yaoshiyizhu commented Mar 25, 2017

执行php artisan db:seed
报 Class "Db" not found
这个是什么问题,已经执行了composer dump-autoload

@yaoshiyizhu

This comment has been minimized.

yaoshiyizhu commented Mar 31, 2017

已经找到了,写错了

@potato520

This comment has been minimized.

potato520 commented Mar 31, 2017

学习一下

@ShuiPingYang

This comment has been minimized.

ShuiPingYang commented Apr 1, 2017

大哥,写的太好了!我用的是laravel一键安装包,laravel是5.2版本的,环境是wamp64位集成环境。
composer dump-autoload
'composer' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
意思是没安装composer,但是我又vender文件夹。
官方说明是这样的:
我们提供的 Laravel & Lumen 一键安装包是在 Laravel 官方 GitHub 仓库源码的基础上安装了依赖库(也就是已经做过 composer install,已经有了 vendor 目录),对于刚接触 Laravel 或者使用 Composer 受挫的用户能够提供快速上手实践 Laravel 的捷径。

第二个问题:我配置了虚拟主机,访问首页有反应,但是不能访问login,如果使用localhost:8000/login就可以,这个是我哪里没配置好吗?windows, apache ,mysql,php集成环境。
谢谢!

第二个问题已解决,虚拟机没配好。
要改两个地方;
D:\wamp64\bin\apache\apache2.4.18\conf\extra\httpd-vhosts.conf
这里要加上
<VirtualHost *:80>
ServerName laravel5.com
DocumentRoot d:/wamp64/www/laravel-v5.2.15/public
<Directory "d:/wamp64/www/laravel-v5.2.15/public">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require local


然后再C:\Windows\System32\drivers\etc\hosts
127.0.0.1 laravel5.com
::1 laravel5.com
加上这个,就可以了!不信你们试试,我看蛮多人都有这个问题的。

@littlewrite

This comment has been minimized.

littlewrite commented May 5, 2017

看到楼主加载的图片,就知道楼主科学上网之路不是好顺啊。

@johnlui

This comment has been minimized.

Owner

johnlui commented May 5, 2017

@littlewrite 这是 Github 自己爬取放到自己服务器的,跟我没关系呦

@johnlui

This comment has been minimized.

Owner

johnlui commented May 5, 2017

@littlewrite 经过我对图片链接的简单修改,Github 重新pa爬了图片,图片出来啦

@codeinfo

This comment has been minimized.

codeinfo commented Jun 1, 2017

使用5.4学习的话会有一个 字符串长度的坑
修改文件\app\Providers目录下AppServiceProvider.php
`use Illuminate\Support\Facades\Schema;

public function boot()
{
Schema::defaultStringLength(191);
}`

@PleaseCallMeXiuye

This comment has been minimized.

PleaseCallMeXiuye commented Jun 7, 2017

laravel 5.4
4. 连接数据库
a. 修改数据库之后请ctrl+c断开重新执行 2. php artisan serve

要不是加载之前旧的数据库配置,使劲数据库连接报错

@johnlui

This comment has been minimized.

Owner

johnlui commented Jun 8, 2017

@PleaseCallMeXiuyephp -S 0.0.0.0:1024 可能就没这个问题了呦

@cancerXW

This comment has been minimized.

cancerXW commented Jun 17, 2017

laravel 5.4
b. 怎么使用composer把database目录设置为自动加载?直接使用composer dump-autoload显示为generating optimuzed autoload files 然后再运行php artisan db:seed 就报找不到异常了,个人怀疑是composer使用不正确?我的尝试结果如图
qq 20170617150113

@johnlui

This comment has been minimized.

Owner

johnlui commented Jun 17, 2017

@cancerXW table 写成了 tabke

@cancerXW

This comment has been minimized.

cancerXW commented Jun 19, 2017

@johnlui 谢谢,没注意到代码的问题。还有个问题Article::create和Article::created 有什么区别?为什么laravel 前面我这出现了数据加不进去,也没提示报错。回去查找对比了下,发现多个d,这个是ide自己联想出来的。这两个有什么区别么?

@meok23

This comment has been minimized.

meok23 commented Jun 30, 2017

@lz408682690 防火墙问题吧。

@xbc1992

This comment has been minimized.

xbc1992 commented Jul 22, 2017

Laravel 5.4 migrate时报错: Specified key was too long error
https://segmentfault.com/a/1190000008416200

@TerryGeng

This comment has been minimized.

TerryGeng commented Aug 12, 2017

@Pine0302 关于article和articles的问题,在laravel的官方的eloquent文档中有解释(文中拿一个叫Flight的model举例):

Note that we did not tell Eloquent which table to use for our Flight model. By convention, the "snake case", plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the Flight model stores records in the flights table.

上文的意思是如果没有指定model对应的table name,会自动认为model名称的复数形式是table name。也可以通过在class Flight添加
protected $table = 'my_flights';
来指定table name。

@johnlui johnlui closed this Nov 11, 2017

@AesopL

This comment has been minimized.

AesopL commented Nov 16, 2017

@zl02655931 在第六步执行php artisan make:migration create_article_table的时候,把article改为articles

@johnlui

This comment has been minimized.

Owner

johnlui commented Nov 16, 2017

@AesopL 和这个并没有绝对的关系,对表名称的定义在这个文件里面:Schema::create('articles', function(Blueprint $table)

@zhongzhaoli

This comment has been minimized.

zhongzhaoli commented Nov 22, 2017

写的很好,谢谢你!

@liulijackwen

This comment has been minimized.

liulijackwen commented Dec 20, 2017

在make:auth和migrate的地方就卡住了。。。make:auth后在login里输入邮箱密码后报错
SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) (SQL: select * from users where email = A@b limit 1)

命令行php artisan migrante报错
In Connection.php line 664:

SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) (SQL: select * from informat
ion_schema.tables where table_schema = blog and table_name = migrations)

In Connector.php line 67:

SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)

请指教。。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment