【问题标题】:insert a post with category and tags using laravel corcel?使用 laravel corcel 插入带有类别和标签的帖子?
【发布时间】:2022-01-23 21:18:14
【问题描述】:

我正在使用 Corcel^5.0.0 和 Laravel 8 在 wordpress 中创建帖子。我正在尝试插入带有类别 ID 和标签的新帖子。

post 工作正常,但插入类别时出现错误 调用未定义的方法 Illuminate\Database\Eloquent\Relations\BelongsToMany::associate()

        $post = new Post;
        $post->post_title = $title;
        $post->post_content = $content;
        $post->post_status = $post_status;
        $post->taxonomies('categories')->associate($cat_id);
        $post->save();

我的控制器和 Post 模型类中也有这个:

use Corcel\Post as Corcel;

【问题讨论】:

    标签: php laravel wordpress categories corcel


    【解决方案1】:

    尝试使用 belongsToMany 方法

    ->attach($cat_id) instead of ->associate which is for a belongsTo relationship
    
    ->sync($cat_ids_array)
    
    ->detach($cat_id)
    

    【讨论】:

    • 这些方法不起作用
    猜你喜欢
    • 2017-06-29
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多