在学习Laracast教程时,我得到的输出与他们得到的不匹配.我做错了什么?


When going through Laracast Tutorials, the output I get doesn't match what they get. What am I doing wrong?

我正在学习laracast教程,但是我的tinker看起来一点也不像laracast,也不会做我想让它做的事情

    Psy Shell v0.5.1 (PHP 5.5.12 ÔÇö cli) by Justin Hileman
>>> $article = App'Article::create{['title' => 'New Article', 'body' => 'New body', 'published_at' => Carbon'Carbon::now()]);
PHP Parse error: Syntax error, unexpected '{' on line 1
>>> $article = App'Article::create{['title' => 'New Article', 'body' => 'New body', 'published_at' => Carbon'Carbon::now()]);
PHP Parse error: Syntax error, unexpected '{' on line 1
>>> $name
PHP error:  Undefined variable: name on line 1
>>> $article = new App'Article;
=> App'Article {#655}
>>> $article
=> App'Article {#655}
>>> new App'User;
=> App'User {#648}
>>> $article = new App'Article;
=> App'Article {#651}
>>>

这就是它所显示的有人知道什么可能是错的吗?

编辑:新的错误,不能让我继续

    $article->save();
    Illuminate'Database'QueryException with message 'SQLSTATE[HY000]: General error: 1 no such table: articles (SQL: insert into "articles" ("body", "published_at", "title", "updated_at", "created_at") v
    alues (Lorem ipsum, 2015-07-22 15:20:49, My First Article, 2015-07-22 15:27:15, 2015-07-22 15:27:15))'

edit2:试过文章查找行,结果显示

    Psy Shell v0.5.1 (PHP 5.5.12 ÔÇö cli) by Justin Hileman
>>> $article = new App'Article;
=> App'Article {#652}
>>> $article - App'Article::find(1);
Illuminate'Database'QueryException with message 'SQLSTATE[HY000]: General error: 1 no such table: articles (SQL: select * from "articles" where "articles"."id" = 1 limit 1)'

我的config/database.php文件的内容是:

<?php
return [
/*
|--------------------------------------------------------------------------
| PDO Fetch Style
|--------------------------------------------------------------------------
|
| By default, database results will be returned as instances of the PHP
| stdClass object; however, you may desire to retrieve records in an
| array format for simplicity. Here you can tweak the fetch style.
|
*/
'fetch' => PDO::FETCH_CLASS,
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => 'sqlite',
/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/
'connections' => [
    'sqlite' => [
        'driver'   => 'sqlite',
        'database' => storage_path().'database.sqlite',
        'prefix'   => '',
    ],
    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'forge'),
        'username'  => env('DB_USERNAME', 'forge'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],
    'pgsql' => [
        'driver'   => 'pgsql',
        'host'     => env('DB_HOST', 'localhost'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset'  => 'utf8',
        'prefix'   => '',
        'schema'   => 'public',
    ],
    'sqlsrv' => [
        'driver'   => 'sqlsrv',
        'host'     => env('DB_HOST', 'localhost'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset'  => 'utf8',
        'prefix'   => '',
    ],
],
/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/
'migrations' => 'migrations',
/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/
'redis' => [
    'cluster' => false,
    'default' => [
        'host'     => '127.0.0.1',
        'port'     => 6379,
        'database' => 0,
    ],
],
];

你的例子来自Windows命令提示符

你必须记住,Laravel是在美国创建的,那里(几乎)所有的技术人员都是苹果人。如果你不使用Mac,你就不酷;)

所以让我们忘记苹果控制台,从Windows的角度来看。

<>之前>>> $article = new App' article=> App'Article {#658}之前

App'Article{#658}表示tinker准备了一个带有#658 id的空对象。

苹果孵化的Tinker,显示如下:

<>之前>>文章美元;对象(应用程序'文章)('increment ' => true;'timestamp ' => true;'exists' => false)之前

命令提示符不自动填充对象,它保持为空。

可能的原因是(但这只是我的猜测),递增的时间戳无论如何都是自动完成的,所以为什么要麻烦呢?

但是这些值,如OSX Tinker所示,是存在的。

如果您希望看到它们,请在命令提示符中运行:

<>之前>> var_dump ($);之前

…但请准备好迎接大量的数据。

如果您检查该数据,您将发现缺少对象元素-就像我所做的那样:

<>之前["增加"]=> bool(真正的)("时间戳")=> bool(真正的)["存在"]=> bool(假)之前

所以,缺少的对象元素在那里,它们不会显示在命令提示符中。

现在,开始填充新创建的对象:

<>之前>>> $article->title = '新标题';=> "新标题">>> $article->body = 'Some body…哇! ';=>"有人……哇!"之前

…你会看到你的对象突然活了起来:

<>之前>>>文章美元=> App'Article {#659标题:"新标题";某人:"某人……哇!"}之前

你也可以把它看作一个数组:

<>之前>>>文章-> toArray ();=> ["title" => "New title","body" => "Some body…"哇!"]之前

所以,正如你所看到的,它是相同的东西,只是在不同的包装。

问题是,当你学习的时候,每一件小事都会让你恼火,让你偏离正轨。所以,保持冷静,坚持下去——Laravel是非常值得的。即使它只是重新发明同一个轮子……第n次了。但它做得很好。

我重新创建了您提到的错误。问题可能出在迁移文件中。在up()和down()方法中都应该使用

模式:创建("文章"…

模式:下降("文章"…

。如果你用"article"来代替,你就会遇到上述错误。请记住,表是为许多文章,所以它的名字是"文章",而模型描述一篇文章-这就是为什么它的名字是"文章"-但它仍然读取/写入记录从/到"文章"表。

相关文章: