当前位置: 首页 > 知识库问答 >
问题:

如何向laravel迁移添加随机字符串列

洪哲彦
2023-03-14

在我的laravel应用程序中有一个名为“threads”的表,我想在其中添加一个名为key的新列,其中包含长度为5的随机字符串。我在CLL上运行了php artisan migrate:make add_key_to_threads来创建迁移文件。在生成的迁移文件中,我使用了up函数

Schema::table('threads', function($table){
            $table->str_random('key', 5);
        })

添加新列,然后在CLL上运行php artisan migrate。很明显,方法str_random()不存在(在搜索资源时发现),因为我不断得到错误“[BadMethodCallException]方法str_random不存在”。有没有人能指导我正确地做这件事。

共有3个答案

印子平
2023-03-14

给你:

<?php

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

class AddRandomStringKeyToThreadsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('threads', function (Blueprint $table) {
            $table->string('key');
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('threads', function (Blueprint $table) {
            $table->dropColumn('key');
        });
    }
}

我已经更改了$表-

然后做这个路由只是为了更新现有的行...这对我有用...也许你需要为你的桌子修改这个。但至少你会知道怎么做。

Route::get('/update-table', function () {
    $count = DB::table('threads')->count();

    while ($count > 1){
        DB::table('threads')
            ->where('id', '=', $count)
            ->update(['key' => str_random(5)]);
        $count--;
    }

    return 'DB updated';
});

然后需要为每个新线程创建一个随机字符串

陈实
2023-03-14

据我所知这是可能的。在迁移过程中,您需要创建如下普通字符串列:

$table->string('key', 5);

然后在创建新模型时,应运行:

$model->key = str_random(5);

假设在创建模型时使用Elount,则可以使用以下语法:

$thread = new Thread();
// below are sample fields you normally save for this thread
$thread->field_1 = 'value1';
$thread->field_2 = 'value2';
// this is the random key you set
$thread->key = str_random(5);
// now you save thread in database
$thread->save();
和和煦
2023-03-14

Schema::table仅用于更新数据库表,而不使用相应的SQL语句。在SQL中,没有自动用随机字符串填充表的列类型。因此,列类型必须是string。请参见Laravel 5.5中列出的所有类型https://laravel.com/docs/5.5/migrations#columns

$table->string('key', 5);

下一个问题是键是否必须是唯一的,如果是,则可以附加unique()函数:

$table->string('key', 5)->unique();

如果您已经有了数据,那么您必须在第二步中输入这些数据,然后,输入所有密钥。

您有两种选择来填充列中的随机数据,使用PHP(Laravel)或SQL。使用SQL时,您必须编写一个函数,该函数可以根据您的SQL Server类型而有所不同,例如:SQL Server列的默认随机10个字符串值

使用Laravel Eloqent,您不必考虑SQL服务器。

$threads      = new Threads();
$threads->key = str_random(5);
$threads->save();

如果您使用-

/* only if you need a unique Key */
$threads     = new Threads();

while (true)
{
    try
    {
        $threads->key = str_random(5);
        $threads->save();
        //  We could save now we break out of the loop
        break;
    }
    catch (Exception $e)
    {
        //  Could not save, try it again
    }
}

对于现有行,可以按如下方式更改迁移:

public function up()
{
    Schema::table('threads', function (Blueprint $table)
    {
        $table->string('key', 5);
    });

    $existing_threads = Threads::where('key', '')->get();

    foreach($existing_threads as $threads)
    {
        $threads->key = str_random(5);
        $threads->save();                
    }
}

必须先退出模型线程,然后才能执行迁移

 类似资料:
  • 我有一些表,我想在这个表中添加新的列。我想添加默认值为空的字符串列。 我试着这样加上去 或 但我有一个错误

  • 我想在laravel中现有的表中添加一些新列。 我已经在谷歌上搜索过了,在这些搜索之后,我已经使用命令创建了迁移。 将列添加到用户。php 创建之后,我运行这个命令。 但也犯了同样的错误: 基本表或视图已存在:1050个表“用户”已存在(SQL:create table(int unsigned not null自动递增主键,varchar(255)not null,varchar(255)not

  • 我不知道如何使用Laravel框架将新列添加到我现有的数据库表中。 我尝试使用...编辑迁移文件。 在终端中,我执行和。 如何添加新列?

  • 我花了一整天的时间试图弄清楚下面的迁移发生了什么。 这是表的迁移。我的想法是使用“id_stays_abroad”和“user_id”作为外键。 这是添加外键的另一个迁移 当我运行php artisan迁移时,我遇到了以下错误: SQLSTATE[HY000]:一般错误:1215无法添加外键约束(SQL:alter table

  • 当我们在Laravel框架中运行migrate:install时,migrations表是在有两列(migration,batch)的数据库中创建的,无论如何都要添加在时间戳处创建的新列。谢谢

  • 假设我在一个数组列表中添加了2个用户Apple和Orange。然后我想将牛顿添加到苹果的朋友列表中。所以苹果应该有1个朋友,而橙色仍然没有任何朋友,因为我还没有添加他们。 我的问题是,当我把牛顿添加到苹果的朋友中时,牛顿也会出现在orange的朋友中。我只需要一个朋友出现在苹果和橙色没有。 我认为这是我为用户的朋友设置arraylist的方式,但我不确定如何更改它。