php - 使用 corcel 为帖子添加类别
问题描述
我正在使用"jgrossi/corcel": "3.0.0",Laravel 6.16.0
并且PHP 7.4.1
我想将现有类别添加Product
到我在 wordpress 中的新帖子中。但是,我不知道该怎么做。
我可以通过以下方式创建 wordpress 帖子:
$post = new Post();
$post->post_title = $title;
$post->post_content = $msg;
$post->save();
有什么建议可以corcel
用来将现有类别添加到帖子中吗?
感谢您的回复!
解决方案
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
因此,您应该能够使用类别 ID 将帖子链接到类别,如下所示...
$post->taxonomies()->associate($taxonomyId);
您可以在此处找到有关这些类型关系的更多信息https://laravel.com/docs/8.x/eloquent-relationships#updating-belongs-to-relationships
推荐阅读
- uwp - 如何禁用 MediaTransportControls (UWP) 中的下一个和上一个按钮
- html - 尝试将导航栏链接居中引导 4
- spring-boot - 使用 springboot 的 Restful LDAP 身份验证服务
- ios - Xcode 配置文件配置问题
- c - 我的关于选择排序的程序有什么问题?
- django - 我可以对图像进行一些预处理并在不保存更改的情况下使用它吗?
- c# - 如何从 XAML 中定义的 ControlTemplate 访问元素(图像)
- casting - 自定义类型转换 - pybind11
- android - 使用 kotlin 在 android studio 中循环播放 mp3?
- java - 如何查看或监控 AWS CodeStar 部署的 Spring 项目应用程序日志?