创建连接的迁移 table

Migration for creating join table

我的 SQL 数据库中有一个名为“projects”的 table,其中有一列“categories”,它是 php 数组的 varchar(255) 表示"['category_1', 'category_2', 'category_3']" 我想做的是将这些类别放入一个单独的 table 中,除了类别名称外,它还由一个唯一的整数 id 组成,然后使用连接 table 将其连接到 many-to-many 关系中的“项目”。

所以这将是一个将执行以下操作的迁移:

  1. 创建一个名为“categories_projects”的联接 table“
  2. 创建一个名为“类别”的 table,它只包含唯一 ID 和类别标题
  3. 在类别中插入一行,因此“category_1”、“category_2”、“category_3”
  4. 在“项目”table 中查找任何现有行,并根据上面提到的 varchar(255) 字段“类别”,在连接 table 中创建一个行来连接它到其各自的类别。 我目前拥有的:
    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class CreateCategoriesTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('categories', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('parent_id')->nullable();
                $table->string('title');
                $table->timestamps();
                $table->softDeletes();
            });
            DB::table('categories')->insert(
                [
                  'title' => 'category_1'
                ], [
                  'title' => 'category_2'
                ], [
                  'title' => 'category_3'
                ], [
                  'title' => 'category_4'
                ], [
                  'title' => 'category_5'
                ]
            );
            Schema::create('categories_projects', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('project_id');
                $table->unsignedInteger('category_id');
                $table->timestamps();
                $table->softDeletes();
            });
            // This doesn't work but it's a representation of what I'm trying to do
            // $categories = DB::rawQuery('select * from categories');
            // foreach ($categories as $category) {
            //    $projects = DB::rawQuery('select * from projects where projects.category like %$category['title']');
            //    foreach($projects as $project) {
            //      DB::table(categories_projects)->insert(['project_id' => $project['id'], 'category_id' => $category['id']]);
            //    }
            //    
            // }
        }

试试这个:

如果你主table中的分类名称两边都是单引号(比如'category_1')

$categories = DB::rawQuery("select * from categories");
foreach ($categories as $category) {
    $projects = DB::rawQuery("select * from projects where category like '%''" . $category["title"] . "''%' ");

    foreach($projects as $project) {
        DB::table('categories_projects')->insert(['project_id' => $project['id'], 'category_id' => $category['id']]);
    }

}

这就是我最终的结果,似乎我在提问的过程中回答了我自己的问题,但无论如何:

public function up()
    {
        Schema::dropIfExists('categories');
        Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->timestamps();
            $table->softDeletes();
        });
        DB::table('categories')->insert([
            ['title' => 'Addition'],
            ['title' => 'Bathrooms'],
            ['title' => 'Commercial'],
            ['title' => 'Community'],
            ['title' => 'Dental'],
            ['title' => 'Design & Construction'],
            ['title' => 'Facade'],
            ['title' => 'Home Design'],
            ['title' => 'Medical'],
            ['title' => 'Multi-Family'],
            ['title' => 'Office'],
            ['title' => 'Renovation'],
            ['title' => 'Residential'],
            ['title' => 'Restaurant'],
            ['title' => 'Retail'],
            ['title' => 'Student Housing']
        ]);
        Schema::create('categories_projects', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('project_id');
            $table->unsignedInteger('category_id');
            $table->timestamps();
            $table->softDeletes();
        });
        $categories = DB::table("categories")->get();
        foreach ($categories as $category) {
            $category_id = $category->id;
            $projects = DB::table('projects')->where('categories', 'like', '%'.$category->title.'%')->get();
            $category_proj = [];
            foreach ($projects as $project) {
                $project_id = $project->id;
                $category_proj[] = ['project_id' => $project_id, 'category_id' => $category_id];
            }
            DB::table('categories_projects')->insert($category_proj);
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('categories_projects');
        Schema::dropIfExists('categories');
    }