【发布时间】: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