使用 corcel 将类别添加到 post

Using corcel to add a category to a post

我正在使用 "jgrossi/corcel": "3.0.0"Laravel 6.16.0PHP 7.4.1

我想将现有类别 Product 添加到我在 wordpress 中的新 post。但是,我不确定该怎么做。

我可以通过以下方式创建 wordpress post:

        $post = new Post();
        $post->post_title = $title;
        $post->post_content = $msg;
        $post->save();

关于如何使用 corcel 将现有类别添加到 post 的任何建议?

感谢您的回复!

Post 模型与分类模型具有 belongsToMany 关系,如您在此处所见...

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
 */
public function taxonomies()
{
    return $this->belongsToMany(
        Taxonomy::class,
        'term_relationships',
        'object_id',
        'term_taxonomy_id'
    );
}

//Source: https://github.com/corcel/corcel/blob/5.0/src/Model/Post.php
//Lines: 185-196

因此,您应该能够 link post 使用类别 ID 的类别...

$post->taxonomies()->associate($taxonomyId);

您可以在此处找到有关这些类型关系的更多信息 https://laravel.com/docs/8.x/eloquent-relationships#updating-belongs-to-relationships