我正在开发一个laravel项目,每次我更改我的表(添加或删除列)并运行php artisan migrate:refresh
。我收到这个错误:
[Symfony \ Component \ Debug \ Exception \ FatalErrorException]无法使用 写入上下文中的方法返回值
尝试解决方案:
composer dump-autoload
(失败)上一个迁移文件:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id');
$table->string('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
更改了迁移文件:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('post_id');
$table->string('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
我在up函数
中的change文件中添加了user_id答案 0 :(得分:3)
尝试此命令对我有用
php artisan migrate:fresh
答案 1 :(得分:0)
尝试一下 触发此命令
php artisan make:migration add_user_id_to_comments_table --table=comments
然后将创建一个新的迁移文件
$table->integer('user_id')->after('id');
然后使用
php artisan migrate