【问题标题】:Laravel: migrate:refresh deosn't workLaravel:迁移:刷新不起作用
【发布时间】:2018-01-03 10:51:39
【问题描述】:

我从 udemy.com 学习 Laravel,但我遇到了 migrate:refresh 命令的问题。所以,当我尝试使用它时,我会看到信息

数组到字符串的转换 我尝试解决我的问题,所以我犯了新的错误,所以这是我的代码: 进入 UserTableSeeder

    public function run()
{
    App\User::create([
        'name' => 'Kati',
        'email' => 'hello@hello.pl',
        'password' => bcrypt('password'),
        'admin' => 1
    ]);
    App\Profile::create([
        'user_id' => $user->id,
        'avatar' => 'link to avatar',
        'about' => 'Interested description',
        'youtube' => 'youtube.com',
        'facebook' => 'facebook.com'
    ]);
}

进入迁移

    public function up()
{
    Schema::create('profiles', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id');
        $table->text('about');
        $table->string('youtube');
        $table->string('facebook');
        $table->timestamps();
    });
}

    public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->boolean('admin')->default(0);
        $table->rememberToken();
        $table->timestamps();
    });
}

我尝试从 github 克隆我的存储库并刷新,但我遇到了一些问题。

【问题讨论】:

  • 检查$user->id是有效值

标签: php laravel migrate


【解决方案1】:

这应该可行:

public function run()
{
    $user = App\User::create([
        'name' => 'Kati',
        'email' => 'hello@hello.pl',
        'password' => bcrypt('password'),
        'admin' => 1
    ]);

    App\Profile::create([
        'user_id' => $user->id,
        'about' => 'Interested description',
        'youtube' => 'youtube.com',
        'facebook' => 'facebook.com'
    ]);
}

在您的运行语句中,您在App\Profile 创建过程中引用了变量$user->id。但是,它还没有被实例化。 此外,您正在尝试将头像添加到您的个人资料中,因此您应该将其添加到Profile-table 中,或者像我一样,将其从播种器中删除。

【讨论】:

  • 您能否提供有关该错误的更多详细信息?你试过修改后的代码(没有头像)吗?
  • 是的。就像我说的那样,我在没有 Profile 的情况下克隆了我以前的存储库,它也不起作用。 php artisan 迁移工作,也迁移:新鲜。你能看到我的代码吗?链接在我之前的消息中
  • 将文件 database/migrations/2018_01_01_205508_create_post_tag_table.php 中的第 31 行从 Schema::drop(['post_tag']); 更改为 Schema::dropIfExists('post_tag');。上面的代码没有任何问题。
【解决方案2】:

我在播种机中解决了它:

'user_id' =>\App\User::pluck('id')->random();

【讨论】:

    猜你喜欢
    • 2017-07-24
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 2018-06-18
    • 2021-02-08
    相关资源
    最近更新 更多