简体   繁体   English

为文章添加多个类别

[英]Add multiple categories for articles

I need to add several categories for a new article.我需要为一篇新文章添加几个类别。 I will write down everything I do in order:我会按顺序写下我所做的一切:

migration of categories类别迁移

public function up()
    {
        Schema::create('blog_categories', function (Blueprint $table) {
            $table->BigIncrements('id');
            $table->string('title', 128);
            $table->timestamps();
        });
    }

migration of articles物品迁移

public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->BigIncrements('id');
            $table->string('title', 128);
            $table->string('slug', 64);
            $table->string('subtitle', 256)->nullable();
            $table->timestamps();
        });
    }

creating another migration article_blog_category_table创建另一个迁移article_blog_category_table

public function up()
    {
        Schema::create('article_blog_category', function (Blueprint $table) {
            $table->unsignedBigInteger('blog_category_id')->nullable();
            $table->foreign('blog_category_id')
                ->references('id')->on('blog_categories')->onDelete('set null');
            $table->unsignedBigInteger('article_id')->nullable();
            $table->foreign('article_id')
                ->references('id')->on('articles')->onDelete('cascade');
        });
    }

Doing belongsToMany in models在模型中做belongsToMany

article model文章 model

public function blog_categories()
    {
        return $this->belongsToMany('App\Models\BlogCategory');
    }

category model类别 model

public function articles()
    {
        return $this->belongsToMany('App\Models\Article');
    }
}

Next, I write the function for adding an article in the controller (I think there is no need to write the function for adding a category, everything is clear there)接下来我在controller中写了加文章的function(我觉得加分类的function就不用写了,那里都清楚)

Articles controller文章 controller

public static function saveArticle(Request $request) {
        $validator = Validator::make($request->all(), [
            'blog_category_id' => 'required|numeric',
            'title' => 'required|max:128',
            'slug' => 'required|unique:articles|max:64',
            'subtitle' => 'max:256',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'message' => $validator->errors()->first()
            ], 422);
        }

        $article = new Article();

        $blog_category = BlogCategory::where('id', $request->blog_category_id)->first();

        if(!$blog_category){
            return response()->json([
                'message' => 'Blog category not found'
                ], 404);
        }

        $article->blog_category_id = $request->blog_category_id;
        $article->title = $request->title;
        $article->slug = $request->slug;
        $article->subtitle = $request->subtitle;

        $article->save();

        return Article::where('slug', $article->slug)->first();
    }

I have a method in the function to add one category.我在function有方法添加一个类。 The question of how to add here so that you can add several categories, I cannot figure it out.怎么在这里添加,这样你就可以添加几个类别的问题,我想不通。 You need something like $article->blog_categories()->attach($request->blog_category_id);你需要像$article->blog_categories()->attach($request->blog_category_id); but how to apply it correctly?但如何正确应用呢?

Your naming convention is complicating your task.您的命名约定使您的任务复杂化。

Rename table in categories migration:重命名类别迁移中的表:

Schema::create('categories', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->timestamps();
});

Also for simplicity, rename the joint (pivot) table同样为了简单起见,重命名联合(数据透视)表

Schema::create('article_category', function (Blueprint $table) {
    // You don't need a table id here
    $table->foreignId('category_id')->index();
    $table->foreignId('article_id')->index();
    $table->unique(['article_id', 'category_id']);
    // You also don't need timestamps
});

Defining relationships in the models:在模型中定义关系:

// Article model
public function categories()
{
    return $this->belongsToMany(\App\Models\Category::class);
}

// Category model
public function articles()
{
    return $this->belongsToMany(\App\Models\Article::class);
}

Article controller文章 controller

public function store() // No need to make the function static
{
    $data = validator(request()->all(), [
        // To save many categories, send them as an array   
        'categories' => 'array',
        'categories.*' => [\Illuminate\Validation\Rule::exists('categories', 'id')], // Checks if category id is in the db
        'title' => 'required|max:128',
        'slug' => 'required|unique:articles|max:64',
        'subtitle' => 'string',
    ])->validate();

    $article = Article::create(
        \Illuminate\Support\Arr::except($data, 'categories');
    );

    // Save categories
    $article->categories()->attach($data['categories']);

    return $article;
}

According to Documentation Many To Many Relationships Attaching / Detaching根据文档多对多关系附加/分离

//You can pass an array of id's categories in attach method:
$article->blog_categories()->attach($categories_ids_array);

/* 
*if you want to pass more columns value you can pass an associative array of 
*column names with their values e.g attach($categories ids array, an array of 
*more columns with their values)
*/

$article->blog_categories()->attach($categories_ids_array, ['column_name' => 
$column_values]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM