【问题标题】:Laravel 4.2 and migrate make not workingLaravel 4.2 和 migrate 无法正常工作
【发布时间】:2015-11-06 03:46:39
【问题描述】:

我根据《Laravel 4 入门》一书创建了一个项目。

所以,我在 app/models/ 中创建了两个文件 - Cat.php 和 Breed.php,内容如下:

猫.php

<?php

class Cat extends Eloquent {
    protected $fillable = array('name','date_of_birth','breed_id');

    public function breed() {
        return $this->belongsTo('Breed');
    }
}

和 Breed.php

<?php

class Breed extends Eloquent {
    public $timestamps = false;

    public function cats()
    {
        return $this->hasMany('Cat');
    }
}

之后,我使用命令php artisan migration:make create_cats_and_breeds_table

好的,应该在 app/database/migrations 中生成文件。是的。

但是,它的内容和书中的不一样……

在书中:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddCatsAndBreedsTable extends Migration {

    public function up()
    {
        Schema::create('cats', function($table)
        {
            $table->increments('id');
            $table->string('name');
            $table->date('date_of_birth');
            $table->integer('breed_id')->nullable();
            $table->timestamps();
        })
        Schema::create('breeds', function($table)
        {
            $table->increments('id');
            $table->string('name');
        })
    }

    public function down()
    {
        Schema::drop('cats');
        Schema::drop('breeds');
    }

}

我的代码:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddCatsAndBreedsTable extends Migration {

    public function up()
    {
        //
    }

    public function down()
    {
        //
    }

}

发生了什么?

【问题讨论】:

  • 你拥有的代码只是骨架。在 up 方法中输入列定义并运行迁移命令。
  • @CannotFindSymbol ,我知道这个骨架,但它是由命令生成的。 A 应该按照书中的方式生成。
  • php artisan generate:migration create_posts_table --fields="title:string, body:text" 这可能对你有帮助。祝你好运

标签: php laravel laravel-4 eloquent laravel-migrations


【解决方案1】:

migration:make 命令对您的模型一无所知。它只是创建一个存根,您需要为您的表填充列定义。

【讨论】:

  • hmmm,不过按照书上的说法,你应该会生成完整的代码
  • 什么书?要么你看错了,要么这本书在撒谎。 Migrate 命令从未为您生成列定义。这是不可能的 - 在 Eloquent 中,您没有在模型中定义字段列表,因此它不知道您的模型具有哪些属性或它们的类型可能是什么。请注意,您也没有将模型名称传递给命令,因此这是不可能的另一个原因。
【解决方案2】:

https://github.com/laracasts/Laravel-4-Generators

提供一些额外的工匠命令,您可以使用这些命令来指定您的字段以生成迁移文件。

php artisan generate:migration create_posts_table --fields="title:string, body:text"

【讨论】:

    猜你喜欢
    • 2014-09-30
    • 2015-01-12
    • 2015-02-20
    • 1970-01-01
    • 2016-04-11
    • 2013-06-07
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    相关资源
    最近更新 更多