Laravel 4 系列入门教程(一)【最适合中国人的Laravel教程】
基于最新 Laravel 5.2 的 2016 版教程已经发布到 Github:https://github.com/johnlui/Learn-Laravel-5/issues/4
十分建议学习 5.2,4.2 早已不再维护。
向 Laravel 4 – simple website with backend tutorial – Part 1 致敬,本教程部分内容翻译自此文章。
每一个教程完成,我将会git commit一次。
示例代码见 https://github.com/johnlui/Learn-Laravel-4
大家在任何地方卡住,最快捷的解决方式就是去看我的示例代码。
推荐 Laravel 4.2 中文文档 http://laravel-china.org/docs
0. 默认条件
本文默认你已经有配置完善的PHP+MySQL运行环境,懂得PHP网站运行的基础知识。跟随本教程走完一遍,你将会得到一个基础的包含登录的简单blog系统,并将学会如何使用一些强大的Laravel插件和composer包(Laravel插件也是composer包)。
软件版本:PHP 5.4+,MySQL 5.1+
本文不推荐完全不懂PHP与MVC编程的人学习。本文不是 “一步一步跟我做” 教程。本文需要你付出一定的心智去解决一些或大或小的隐藏任务,以达到真正理解 Laravel 运行逻辑的目的。
1. 安装
许多人被拦在了学习Laravel的第一步,安装。并不是因为安装教程有多复杂,而是因为【众所周知的原因】。在此我推荐一个composer全量中国镜像:http://pkg.phpcomposer.com/。推荐以 “修改 composer 的配置文件” 方式配置。
镜像配置完成后,切换到你想要放置该网站的目录下,运行命令:
composer create-project laravel/laravel learnlaravel 4.2.11
为了大家学习的方便,请暂时使用 4.2.11 版本进行入门。春节假期我会抽时间写一下 Laravel 5 的入门教程。4.2.11 的 Github 下载地址为:https://github.com/laravel/laravel/archive/v4.2.11.zip
然后,稍等片刻,当前目录下就会出现一个叫 learnlaravel 的文件夹,这时候如果你通过浏览器访问 learnlaravel/public/ 目录,基本都会显示 Error in exception handler. ,这是因为 learnlaravel/app/storage 目录没有 777 权限,设置好权限即可看见页面如下图:
恭喜你~Laravel安装成功!
不想配置镜像的同学,可以使用 Laravel 界非常著名的超超搞得安装神器:https://github.com/overtrue/latest-laravel
2. 必要插件安装及配置
我们使用著名的Sentry插件来构建登录等权限验证系统。
打开 ./composer.json ,变更为:
"require": { "laravel/framework": "4.2.*", "cartalyst/sentry": "2.1.4" },
然后,在项目根目录下运行命令
composer update
然后稍等一会儿,它会提示 cartalyst/sentry 2.1.4安装完成。
同理,我们将安装一个开发用的非常强大的插件,way/generators,这是它在composer库中的名字。在 composer.json中增加:
"require-dev": { "way/generators": "~2.0" },
和“require”同级,放在下面,不是里面哦~。
运行 composer update,之后在 ./app/config/app.php 中 恰当的位置 增加配置:
'Way\Generators\GeneratorsServiceProvider'
安装完成过,在命令行中运行 php artisan,就可以看到这个插件带来的许多新的功能。
有人会问,为什么用了国内镜像还是如此之慢?其实composer在update的时候最慢的地方并不是下载,而是下载之前的依赖关系解析,由于Laravel依赖的composer包非常之多,PHP脚本的执行速度又比较慢,所以每次update等个两三分钟很正常,习惯就好。
3. 数据库建立及迁移
数据库配置文件位于 ./app/config/database.php,我们需要把“connections”中的“mysql”项改成我们需要的配置。下面是我的配置:
'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'laravel', 'username' => 'root', 'password' => 'password', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => 'l4_', ),
prefix为表前缀,这个Laravel会帮我们自动维护,大胆写上不用担心。
这时候你需要去数据库建立此数据库,然后在命令行中输入:
php artisan migrate --package=cartalyst/sentry
执行完成后,你的数据库里就有了5张表,这是sentry自己建立的。sentry在Laravel4下的配置详情见 https://cartalyst.com/manual/sentry#laravel-4,我大致说一下:
在 ./app/config/app.php 中 相应的位置 分别增加以下两行:
'Cartalyst\Sentry\SentryServiceProvider',
'Sentry' => 'Cartalyst\Sentry\Facades\Laravel\Sentry',
权限系统的数据库配置到此为止。
我们的简单blog系统将会有两种元素,Article和Page,下面我们将创建articles和pages数据表,命令行运行:
php artisan migrate:make create_articles_table --create=articles php artisan migrate:make create_pages_table --create=pages
这时候,去到 ./app/database/migrations,将会看到多出了两个文件,这就是数据库迁移文件,过一会我们将操作artisan将这两个文件描述的两张表变成数据库中真实的两张表,放心,一切都是自动的。
下面,在***_create_articles_table.php中修改:
Schema::create('articles', function(Blueprint $table) { $table->increments('id'); $table->string('title'); $table->string('slug')->nullable(); $table->text('body')->nullable(); $table->string('image')->nullable(); $table->integer('user_id'); $table->timestamps(); });
在***_create_pages_table.php中修改:
Schema::create('pages', function(Blueprint $table) { $table->increments('id'); $table->string('title'); $table->string('slug')->nullable(); $table->text('body')->nullable(); $table->integer('user_id'); $table->timestamps(); });
下面,就是见证奇迹的时刻,在命令行中运行:
php artisan migrate
这时候数据库中的articles表和pages表就建立完成了。
4. 模型 Models
接下来我们将接触Laravel最为强大的部分,Eloquent ORM,真正提高生产力的地方,借用库克的话说一句,鹅妹子英!
我们在命令行运行下列语句以创建两个model:
php artisan generate:model article php artisan generate:model page
这时候,在 app/models/ 下就出现了两个文件 Article.php 和 Page.php,这是两个 Model 类,他们都继承了Laravel提供的核心类 \Eloquent。这里需要强调一下,用命令行的方式创建文件,和自己手动创建文件没有任何区别,你也可以尝试自己创建这两个 Model 类哦。
Model 即为 MVC 中的 M,翻译为 模型,负责跟数据库交互。在 Eloquent 中,数据库中每一张表对应着一个 Model 类。
如果你从其他框架转过来,可能对这里一笔带过的 Model 部分很不适应,没办法,是因为 Eloquent 实在太强大了啦,真的没什么好做的,继承一下 Eloquent 类就能实现很多很多功能了。详见 Eloquent 系列教程:深入理解 Laravel Eloquent(一)——基本概念及用法
5. 数据库填充
分别运行下列命令:
php artisan generate:seed page php artisan generate:seed article
这时,在 ./app/database/seeds/ 下就出现了两个新的文件,这就是我们的数据库填充文件。Laravel提供自动数据库填充,十分方便。
generator默认使用Faker\Factory作为随机数据生成器,所以我们需要安装这个composer包,地址是 https://packagist.org/packages/fzaninotto/faker ,跟generator一起安装在 require-dev 中即可。具体安装请自行完成,可以参考Sentry和Generator,这是第一次练习。
接下来,分别更改这两个文件:
Article::create([ 'title' => $faker->sentence($nbWords = 6), 'slug' => 'first-post', 'body' => $faker->paragraph($nbSentences = 5), 'user_id' => 1, ]);
Page::create([ 'title' => $faker->sentence($nbWords = 6), 'slug' => 'first-page', 'body' => $faker->paragraph($nbSentences = 5), 'user_id' => 1, ]);
然后,我们需要在 DatabaseSeeder.php 中增加两行,让Laravel在seed的时候会带上我们新增的这两个seed文件。
$this->call('ArticleTableSeeder'); $this->call('PageTableSeeder');
下面就要真正的把数据填充进数据库了:
php artisan db:seed
操作完成以后去数据库看看,数据已经填充进去了,article和page各10行。
接下来做什么?Laravel 4 系列入门教程(二)
评论:
2015-06-11 18:32
只是文中下面这段代码,是不是粘贴重复了,和数据表结构对应不上啊。
Article::create([
'title' => $faker->sentence($nbWords = 6),
'slug' => 'first-post',
'body' => $faker->paragraph($nbSentences = 5),
'user_id' => 1,
]);
Page::create([
'title' => $faker->sentence($nbWords = 6),
'slug' => 'first-page',
'body' => $faker->paragraph($nbSentences = 5),
'user_id' => 1,
]);
2015-04-28 14:45
我是在github下载了 faker 的安装包解压到 vendor 目录下,然后又在 app\config\app.php 文件的 'providers' 数组中添加了 'Fzaninotto\Faker\FakerServiceProvider'。就是不行,一直报错“ymfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN)
Class 'Fzaninotto\Faker\FakerServiceProvider' not found ” ;求详细安装教程!
2015-04-20 23:23
- Installing laravel/laravel (v4.2.11)
Downloading: 100%
Failed to download laravel/laravel from dist: RecursiveDirectoryIterator::__construct(learnlaravel/): failed to open dir: No such file or directory
Now trying to download from source
安装总是提示这个错误。
2015-04-10 18:16
Fatal error: require(): Failed opening required '/webserver/program/laravel/bootstrap/../vendor/autoload.php' (include_path='.:/usr/share/php:/usr/share/pear') in /webserver/program/laravel/bootstrap/autoload.php on line 16
安装了没有vendor 目录
2015-03-27 14:24
执行了php artisan mak:console FindUser
然后也写好了
<?php namespace Yoyo\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class UserFind extends Command {
protected $name = 'user:find';
protected $description = 'user:find';
public function __construct()
{
parent::__construct();
}
public function fire()
{
$this->line('hello world');
}
protected function getArguments()
{
return [
//['example', InputArgument::REQUIRED, 'An example argument.'],
];
}
protected function getOptions()
{
return [
//['example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null],
];
}
}
怎么将他添加到php artisan user:find来执行,
2015-03-26 17:34
$ php artisan migrate --package=cartalyst/sentry
**************************************
* Application In Production! *
**************************************
Do you really wish to run this command? yes
[PDOException]
SQLSTATE[HY000] [2002] 目芫薹印
migrate [--bench[="..."]] [--database[="..."]] [--force] [--path[="..."]] [--pac
kage[="..."]] [--pretend] [--seed]
老师您好,如果wamp处于关闭状态只要执行 : $ php artisan migrate --package=cartalyst/sentry 这个命令就会出现上面这个问题,
如果 打开 wamp 那就会出现:
$ php artisan migrate --package=cartalyst/sentry
**************************************
* Application In Production! *
**************************************
Do you really wish to run this command? yes
[PDOException]
SQLSTATE[HY000] [1049] Base 'laravel' inconnue
migrate [--bench[="..."]] [--database[="..."]] [--force] [--path[="..."]] [--pac
kage[="..."]] [--pretend] [--seed]
这个问题,请问是怎么回事啊?我已经配置了N遍了
2015-03-02 08:16
composer create-project laravel/laravel blog 4.2.11
老是出现这个异常呢,
[Symfony\Component\Process\Exception\RuntimeException]
The process has been signaled with signal "11".
还有,laravel 5 有教程吗!!
2015-02-25 03:37
PHP Fatal error: Class 'Fzaninotto\Faker\FakerServiceprovider' n
Apache2.2\htdocs\laravel\bootstrap\compiled.php on line 4481
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErro
ssage":"Class 'Fzaninotto\\Faker\\FakerServiceprovider' not found
pache2.2\\htdocs\\laravel\\bootstrap\\compiled.php","line":4481}}
D:\Apache2.2\htdocs\laravel>
老师:如上所示错误该怎么处理,拜谢
2015-02-11 10:42
wuspdeMacBook-Air:laravel-master wusp$ composer install
Loading composer repositories with package information
Installing dependencies (including require-dev)
[Composer\Downloader\TransportException]
The "http://pkg.phpcomposer.com/repo/packagist/p/illuminate/pagination.json
" file could not be downloaded: php_network_getaddresses: getaddrinfo faile
d: nodename nor servname provided, or not known
failed to open stream: php_network_getaddresses: getaddrinfo failed: nodena
me nor servname provided, or not known
同时在命令行使用 php artisan serve命令时出现以下反馈:
wuspdeMacBook-Air:laravel-master wusp$ php artisan
Warning: require(/Applications/Web/laravel-master/bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in /Applications/Web/laravel-master/bootstrap/autoload.php on line 17
Fatal error: require(): Failed opening required '/Applications/Web/laravel-master/bootstrap/../vendor/autoload.php' (include_path='.:') in /Applications/Web/laravel-master/bootstrap/autoload.php on line 17
请问这个问题是如何引发的,该如何解决呢?
谢谢!
2015-02-10 10:36
exception "BadMethodCallException" with message "Call to undefined method [package]" in D:\wamp\www\laravel\storage\framework\compiled.php 4346
在composer update 或者 php artisan 后会提示这个错误。我用的是laravel 5
composer.json 文件如下
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"laravel/framework": "5.0.*",
"cartalyst/sentry": "2.1.4"
},
"require-dev": {
"way/generators": "~3.0",
"phpunit/phpunit": "~4.0",
"phpspec/phpspec": "~2.1"
},
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php -r \"copy('.env.example', '.env');\"",
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
}
}
2015-01-29 16:07
总结一下,从安装到现在,遇到了几处难点:
1,安装第一步,“切换到你想要放置该网站的目录下”
一开始不知道网站根目录指的是哪里,还以为是public目录,百度上问也没人回答,google外国网站,才得知是app的父目录,这个难点差点把我挡在laravel门外。如果教程中写明网站的目录就是app的父目录就好了。
2,"require": {
"laravel/framework": "4.2.*",
"cartalyst/sentry": "2.1.4"
},
4.2.*原封不动就这样写。不要写成4.2.11之类的。
3,composer.json就在learnlaravel根目录下。我一开始用的是/learnlaravel/vendor/laravel/framework/src/Illuminate/Support/composer.json,导致安装Sentry等插件失败。
4,View [admin._partials.assets] not found.
不知道admin._partials.assets放在哪个目录下,看评论才知是在\app\views\admin\_partials目录下添加assets.blade.php这个文件,刷新后提示找不到admin._partials.navigation,如法炮制一下就好了。
2015-01-29 11:18
提示:
[InvalidArgumentException]
There are no commands defined in the "generate" namespace.
这个错误!!!,怎么办?
老师 ,帮帮我 ,非常感谢,,,,
2015-01-29 12:53
因为,安装完之后,
php artisan
可以看到
key
key:generate Set the application key
2015-01-29 13:19
在 composer.json 中添加
"require-dev": {
"way/generators": "~2.0"
},
然后,命令行
E:\wamp\www\laravel>composer update
Loading composer repositories with package information
Updating dependecies (including require-dev)
- installing way/generators (2.6.1)
Downloading:connection ... Downloading: 0%
Downloading:20%
Downloading:35%
Downloading:40%
Downloading:55%
Downloading:75%
Downloading:95%
Downloading:100%
Writing lock file
Generating autoload files
Generating optimized class loader
Compiling common classes
Compiling views
E:\wamp\www\laravel>
然后,在 app.php 中
'Way\Generators\GeneratorsServiceProvider',
然后在回到 命令行 ,创建模型,
就提示 我给您第一次发的 错误消息。
2015-01-29 14:17
2015-01-06 19:25
( ! ) Parse error: syntax error, unexpected '[' in G:\wamp\www\laravel\vendor\laravel\framework\src\Illuminate\Support\helpers.php on line 426
Call Stack
# Time Memory Function Location
1 0.0005 366768 {main}( ) ..\server.php:0
2 0.0014 370376 require_once( 'G:\wamp\www\laravel\public\index.php' ) ..\server.php:19
3 0.0017 376744 require( 'G:\wamp\www\laravel\bootstrap\autoload.php' ) ..\index.php:21
4 0.0019 378584 require( 'G:\wamp\www\laravel\vendor\autoload.php' ) ..\autoload.php:17
5 0.0024 398840 ComposerAutoloaderInit65218a41523806b5ded47d41b70cea77::getLoader( ) ..\autoload.php:7
6 0.0140 1067000 composerRequire65218a41523806b5ded47d41b70cea77( ) ..\autoload_real.php:49
426行的代码:
function class_uses_recursive($class)
{
$results = []; /*(这个是第426行)*/
foreach (array_merge([$class => $class], class_parents($class)) as $class)
{
$results += trait_uses_recursive($class);
}
return array_unique($results);
}
2015-01-05 12:41
如这样的关联关系User->Information,是一对一的关系。user的model与information的model都有hasOne()。可是当我使用$user->information->description时会报这样的错Trying to get property of non-object。而使用Eloquent的话,$information = User::find($id)->information;是没问题的。我这样的推断是对吗?
2015-01-05 02:15
<pre>
"require-dev": {
"way/generators": "2.*"
},
并且app.php中也添加了
'Way\Generators\GeneratorsServiceProvider',
composer update --dev也更新成功了,可是执行
Laravel Framework version 4.2.16
Usage:
[options] command [arguments]
Options:
--help -h Display this help message.
--quiet -q Do not output any message.
--verbose -v|vv|vvv Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug.
--version -V Display this application version.
--ansi Force ANSI output.
--no-ansi Disable ANSI output.
--no-interaction -n Do not ask any interactive question.
--env The environment the command should run under.
Available commands:
changes Display the framework change list
clear-compiled Remove the compiled class file
down Put the application into maintenance mode
dump-autoload Regenerate framework autoload files
env Display the current framework environment
help Displays help for a command
list Lists commands
migrate Run the database migrations
optimize Optimize the framework for better performance
routes List all registered routes
serve Serve the application on the PHP development server
tail Tail a log file on a remote server
tinker Interact with your application
up Bring the application out of maintenance mode
workbench Create a new package workbench
asset
asset:publish Publish a package's assets to the public directory
auth
auth:clear-reminders Flush expired reminders.
auth:reminders-controller Create a stub password reminder controller
auth:reminders-table Create a migration for the password reminders table
cache
cache:clear Flush the application cache
cache:table Create a migration for the cache database table
command
command:make Create a new Artisan command
config
config:publish Publish a package's configuration to the application
controller
controller:make Create a new resourceful controller
db
db:seed Seed the database with records
key
key:generate Set the application key
migrate
migrate:install Create the migration repository
migrate:make Create a new migration file
migrate:publish Publish a package's migrations to the application
migrate:refresh Reset and re-run all migrations
migrate:reset Rollback all database migrations
migrate:rollback Rollback the last database migration
queue
queue:failed List all of the failed queue jobs
queue:failed-table Create a migration for the failed queue jobs database table
queue:flush Flush all of the failed queue jobs
queue:forget Delete a failed queue job
queue:listen Listen to a given queue
queue:restart Restart queue worker daemons after their current job.
queue:retry Retry a failed queue job
queue:subscribe Subscribe a URL to an Iron.io push queue
queue:work Process the next job on a queue
session
session:table Create a migration for the session database table
view
view:publish Publish a package's views to the application
</pre>
这里没有generate:model? 求帮助。
2014-12-29 11:56
<pre>
"require-dev": {
"way/generators": "2.*"
},
并且app.php中也添加了
'Way\Generators\GeneratorsServiceProvider',
composer update --dev也更新成功了,可是执行
Laravel Framework version 4.2.16
Usage:
[options] command [arguments]
Options:
--help -h Display this help message.
--quiet -q Do not output any message.
--verbose -v|vv|vvv Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug.
--version -V Display this application version.
--ansi Force ANSI output.
--no-ansi Disable ANSI output.
--no-interaction -n Do not ask any interactive question.
--env The environment the command should run under.
Available commands:
changes Display the framework change list
clear-compiled Remove the compiled class file
down Put the application into maintenance mode
dump-autoload Regenerate framework autoload files
env Display the current framework environment
help Displays help for a command
list Lists commands
migrate Run the database migrations
optimize Optimize the framework for better performance
routes List all registered routes
serve Serve the application on the PHP development server
tail Tail a log file on a remote server
tinker Interact with your application
up Bring the application out of maintenance mode
workbench Create a new package workbench
asset
asset:publish Publish a package's assets to the public directory
auth
auth:clear-reminders Flush expired reminders.
auth:reminders-controller Create a stub password reminder controller
auth:reminders-table Create a migration for the password reminders table
cache
cache:clear Flush the application cache
cache:table Create a migration for the cache database table
command
command:make Create a new Artisan command
config
config:publish Publish a package's configuration to the application
controller
controller:make Create a new resourceful controller
db
db:seed Seed the database with records
key
key:generate Set the application key
migrate
migrate:install Create the migration repository
migrate:make Create a new migration file
migrate:publish Publish a package's migrations to the application
migrate:refresh Reset and re-run all migrations
migrate:reset Rollback all database migrations
migrate:rollback Rollback the last database migration
queue
queue:failed List all of the failed queue jobs
queue:failed-table Create a migration for the failed queue jobs database table
queue:flush Flush all of the failed queue jobs
queue:forget Delete a failed queue job
queue:listen Listen to a given queue
queue:restart Restart queue worker daemons after their current job.
queue:retry Retry a failed queue job
queue:subscribe Subscribe a URL to an Iron.io push queue
queue:work Process the next job on a queue
session
session:table Create a migration for the session database table
view
view:publish Publish a package's views to the application
</pre>
这里没有generate:model? 求帮助。
2014-12-25 20:33
我自己创建的problem表,在seed填充时,它非得去找problems这张表。。。
有没有方法设置不自动寻找表名的复数形式。。
2014-12-24 20:36
php artisan db:seed
出现以下错误
PHP Fatal error: Class 'Article' not found in /Library/WebServer/Documents/work_com/composer/laravel/learnlaravel/app/database/seeds/ArticleTableSeeder.php on line 14
2014-12-25 13:01
Article::create([
'title' => $faker->sentence($nbWords = 6),
'slug' => 'first-post',
'body' => $faker->paragraph($nbSentences = 5),
'user_id' => 1,
]);
2014-12-18 08:22
2014-12-18 08:20
这个实质是在public function up() 里面插入,可以写得更明确些。
2014-12-09 08:37
Do you really wish to run this command?
PHP Fatal error: Class 'Faker\Factory' not found in E:\wamp\www\laravel-master\app\database\seeds\ArticleTableSeeder.php on line 10
PHP Stack trace:
PHP 1. {main}() E:\wamp\www\laravel-master\artisan:0
PHP 2. Symfony\Component\Console\Application->run() E:\wamp\www\laravel-master\artisan:59
PHP 3. Symfony\Component\Console\Application->doRun() E:\wamp\www\laravel-master\vendor\symfony\console\Symfony\Component\Console\Application.php:124
PHP 4. Symfony\Component\Console\Application->doRunCommand() E:\wamp\www\laravel-master\vendor\symfony\console\Symfony\Component\Console\Application.php:193
PHP 5. Illuminate\Console\Command->run() E:\wamp\www\laravel-master\vendor\symfony\console\Symfony\Component\Console\Application.php:889
PHP 6. Symfony\Component\Console\Command\Command->run() E:\wamp\www\laravel-master\vendor\laravel\framework\src\Illuminate\Console\Command.php:100
PHP 7. Illuminate\Console\Command->execute() E:\wamp\www\laravel-master\vendor\symfony\console\Symfony\Component\Console\Command\Command.php:252
PHP 8. Illuminate\Database\Console\SeedCommand->fire() E:\wamp\www\laravel-master\vendor\laravel\framework\src\Illuminate\Console\Command.php:112
PHP 9. DatabaseSeeder->run() E:\wamp\www\laravel-master\vendor\laravel\framework\src\Illuminate\Database\Console\SeedCommand.php:57
PHP 10. Illuminate\Database\Seeder->call() E:\wamp\www\laravel-master\app\database\seeds\DatabaseSeeder.php:15
PHP 11. ArticleTableSeeder->run() E:\wamp\www\laravel-master\vendor\laravel\framework\src\Illuminate\Database\Seeder.php:37
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Class 'Faker\\Factory' not found","file":"E:\\wamp\\www\\laravel-master\\app\\database\\seed
s\\ArticleTableSeeder.php","line":10}}
2014-12-03 19:09
"laravel/framework": "4.2.*",
"cartalyst/sentry": "2.1.4"
},然后执行composer update后,cartalyst/sentry suggests installing happydemon/txt (Required Text helpers when
using the Kohana implementation),啥意思。。
下面还有一些内容是
Writing lock file
Generating autoload files
Generating optimized class loader
Compiling common classes
Compiling views
万分感谢
2014-11-09 16:20
===================
打开 ./composer.json ,变更为:
"require": {
"laravel/framework": "4.2.*",
"cartalyst/sentry": "2.1.4"
},
然后,在项目根目录下运行命令
composer update
==================
报这个错误,查了相关资料,不知道什么意思,楼主能解答下吗?
Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 680855 bytes) in phar:///usr/local/Cellar/composer/1.0.0-alpha8/libexec/composer.phar/src/Composer/Repository/ComposerRepository.php on line 281
2014-11-08 01:05
我在生成数据的时候遇到了这样的错误
PHP Fatal error: Call to undefined method Faker\Factory::isDeferred() in /wwwroot/laravel/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php on line 126
最后原因是:我在app.app加了Faker\Factory这样一个命名空间
2014-11-02 14:51
2014-11-03 10:04
2014-11-03 23:46
我没管,然后删掉了local文件夹并在app.php中providers添加了'Way\Generators\GeneratorsServiceProvider',执行php artisan还是错误,删掉就好用。composer.json里的sentry版本都是从教程中复制过来的(composer.json是建工程之前我自己建的,貌似没问题吧)
有点小伤感
2014-11-01 00:21
解决方法是,先下载Lato字体,然后找到 learnlaravel\app\views 目录下的 hello.php 文件修改:
- /*@import url(//fonts.googleapis.com/css?family=Lato:700);*/
+ /* latin-ext */
@font-face {
font-family: 'Lato';
font-style: normal;
font-weight: 700;
src: local('Lato Bold'), local('Lato-Bold'), url(http://fonts.gstatic.com/s/lato/v11/ObQr5XYcoH0WBoUxiaYK3_Y6323mHUZFJMgTvxaG2iE.woff2) format('woff2');
unicode-range: U+0100-024F, U+1E00-1EFF, U+20A0-20AB, U+20AD-20CF, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
font-family: 'Lato';
font-style: normal;
font-weight: 700;
src: local('Lato Bold'), local('Lato-Bold'), url(http://fonts.gstatic.com/s/lato/v11/HdGTqbEHKKIUjL97iqGpTvY6323mHUZFJMgTvxaG2iE.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215, U+E0FF, U+EFFD, U+F000;
}
或者修改成其他字体。
既然是新手教程,这里也可以提一下。
2014-10-29 18:07
+ 运行 composer update,之后在 ./app/config/app.config 中 'providers' => array 增加配置:
纯新手又不懂英文这步就跳不过去了。。。
Once this operation completes, the final step is to add the service provider. Open app/config/app.php, and add a new item to the providers array.
2014-10-29 21:34
鬼知道这个增加配置是怎么增加的啊?
你这是入门+教程,这样写算是入门+教程?
要不是去看https://github.com/JeffreyWay/Laravel-4-Generators 的安装步骤写清楚了"add a new item to the providers array.",真的新手入门冲着这个适合中国人的标题来看,能明白这一步的配置往哪增加?
还说向http://www.codeforest.net/laravel4-simple-website-with-backend-1 致敬,看看人家怎么写的:EDIT: Some people complained there’s not enough info on installing Sentry, so basicaly you need to add the service provider and the alias to your app/config/app.php file:
如果你的标题不是 入门+教程+最适合中国人,我不会说什么。
2014-10-29 22:41
2014-10-24 22:37
php artisan generate:model page
php artisan generate:seed page
php artisan generate:seed article
运行错误,显示:
[InvalidArgumentException]
There are no commands defined in the "generate" namespace.
已经在composer.json中配置,如下
"require-dev": {
"way/generators": "~2.0",
"fzaninotto/faker": "1.5.*@dev"
},
没有在app.php中写配置,不知道要不要,新手不是很懂。
2014-10-22 16:05
[PDOException]
SQLSTATE[HY000]: General error: 1364 Field 'image' doesn't have a default v
alue
原因分析是:
在***_create_articles_table.php中修改:
Schema::create('articles', function(Blueprint $table)
{
$table->increments('id');
$table->string('title');
$table->string('slug')->nullable();
$table->text('body')->nullable();
$table->string('image')->nullable();
$table->integer('user_id');
$table->timestamps();
});
中 $table->string('image')->nullable(); 设置成了必填项导致。
解决方案我认为有两种:
1: $table->string('image')->nullable(); 修改成——> $table->string('image');
2:数据数组中列添加相应值
Article::create([
'title' => $faker->sentence($nbWords = 6),
'slug' => 'first-post',
'body' => $faker->paragraph($nbSentences = 5),
'image' =>'xxxxxxxxxxxxxxxxxxxxxxxx',
'user_id' => 1,
]);
2014-10-12 10:15
1
'Way\Generators\GeneratorsServiceProvider'
我始终找不到“app.config”这个文件
2014-10-07 22:14
我的mysql中列表没有更新,请问是少了什么步骤吗?
2014-10-07 15:24
2014-10-07 15:46
require-dev 是 Composer 的配置项,意思是开发用插件,线上部署的时候不用。默认的 composer update 是包含 dev 的,所以其实现阶段象征意义大于现实意义。Laravel可能会在部署的时候根据环境变量选择自动加载包,以达到减小命名空间树的目的(猜测)。
2014-10-04 02:17
*********************************
接下来,分别更改这两个文件:
Article::create([
'title' => $faker->sentence($nbWords = 6),
'slug' => 'first-post',
'body' => $faker->paragraph($nbSentences = 5),
'user_id' => 1,
]);
Page::create([
'title' => $faker->sentence($nbWords = 6),
'slug' => 'first-page',
'body' => $faker->paragraph($nbSentences = 5),
'user_id' => 1,
]);
2014-10-04 11:47
这时,在 ./app/database/seeds/ 下就出现了两个新的文件,这就是我们的数据库填充文件。Laravel提供自动数据库填充,十分方便。
2014-11-13 15:46
laravel\app\database\seeds\ArticleTableSeeder.php on line 10
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","me
ssage":"Class 'Faker\\Factory' not found","file":"D:\\web1\\apache\\htdocs\\lear
nlaravel\\app\\database\\seeds\\ArticleTableSeeder.php","line":10}}
)
2016-03-25 10:37
php artisan migrate:make create_pages_table --create=pages
这两句有错误,不应该是migrate:make,而是make:migrate