我在起身和使用 Laravel 时遇到困难。数据库(MySQL)给了我一些错误,但我不确定如何修复它们。我编写了这个简单的迁移来创建歌曲表。
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSongsTable extends Migration
{
// Run the migrations.
public function up()
{
Schema::create('songs', function (Blueprint $table) {
$table->increments('id');
$table->string('title'); /* <- Here is the title column */
$table->text('lyrics')->nullable();
$table->timestamps();
});
}
// Reverse the migrations.
public function down()
{
Schema::drop('songs');
}
}
然后我迁移它,一切似乎都工作正常。
$ php artisan migrate
Migrated: 2015_07_14_150540_create_songs_table
但是当我尝试插入某些内容时,会出现错误。它说我缺少标题列,但据我在上面的代码中看到的,我应该有一个,否则我不会收到任何错误。
$ php artisan tinker
Psy Shell v0.5.1 (PHP 5.5.24 — cli) by Justin Hileman
>>> DB::table('songs')->insert([ 'title' => 'Fall',
'created_at' => new DateTime, 'updated_at' => new DateTime]);
Illuminate\Database\QueryException with message 'SQLSTATE[42S22]:
Column not found: 1054 Unknown column 'title' in 'field list'
(SQL: insert into `songs` (`title`, `created_at`, `updated_at`)
values (Fall, 2015-07-14 15:14:10, 2015-07-14 15:14:10))'
编辑:查看 phpMyAdmin 中的数据库,该表在那里,但标题列丢失。这是我得到的:
我哪里出错了?
最佳答案
该表尚未迁移到数据库中。
请使用
php artisan migrate:refresh
恢复您的表格
关于php - 无法插入 Laravel 数据表中缺少的行 - 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31434073/