Reputation: 5355
I am using "jgrossi/corcel": "3.0.0", Laravel 6.16.0
and PHP 7.4.1
I would like to add an existing category Product
to my new post in wordpress. However, I am not sure how to do this.
I can create a wordpress post the following way:
$post = new Post();
$post->post_title = $title;
$post->post_content = $msg;
$post->save();
Any suggestions how to use corcel
to add an existing category to the post?
Appreciate your replies!
Upvotes: 0
Views: 838
Reputation: 600
The Post model has a belongsToMany relationship to the taxonomies model, as you can see here...
/**
* @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
Therefore you should be able to link the post to a category using the category id like so...
$post->taxonomies()->associate($taxonomyId);
You can find more about these types of relationships here https://laravel.com/docs/8.x/eloquent-relationships#updating-belongs-to-relationships
Upvotes: 1