【问题标题】:how to save a list of tags array in database using eloquent laravel如何使用 eloquent laravel 在数据库中保存标签数组列表
【发布时间】:2023-03-13 18:57:01
【问题描述】:

我将在我的表格中保存带有一些标签的帖子,但我无法保存与此新帖子关联的标签列表。我有一个博客、标签、可标记表,如下所示。

-- auto-generated definition
create table blog
(
    id          int auto_increment
        primary key,
    title       varchar(200)                        null,
    content     text                                null,
    author      varchar(200)                        null,
    category    varchar(200)                        null,
    img_url     varchar(200)                        null,
    created_at  timestamp default CURRENT_TIMESTAMP not null,
    updated_at  timestamp                           null,
    deleted_at  timestamp                           null,
    user_id     int                                 not null,
    category_id int                                 null,
    constraint fk18
        foreign key (user_id) references users (id)
            on update cascade on delete cascade,
    constraint fk19
        foreign key (category_id) references blog (id)
            on update cascade on delete cascade
);

create index blog_index
    on blog (category_id);

create index blog_users_index
    on blog (user_id);

-- auto-generated definition
create table tags
(
    id         int auto_increment
        primary key,
    name       varchar(200)                        null,
    created_at timestamp default CURRENT_TIMESTAMP not null,
    updated_at timestamp                           null,
    deleted_at timestamp                           null
);

-- auto-generated definition
create table taggables
(
    id            int auto_increment
        primary key,
    tag_id        int                                 null,
    taggable_type varchar(512)                        null,
    taggable_id   int                                 null,
    created_at    timestamp default CURRENT_TIMESTAMP not null,
    updated_at    timestamp                           null,
    deleted_at    timestamp                           null,
    constraint fk23
        foreign key (tag_id) references tags (id)
            on update cascade on delete cascade
);

create index taggables_tag_id_index
    on taggables (tag_id);



这是我对这些表格的雄辩模型:

class BaseModel extends Model
{
    protected $table;
    protected $primaryKey;


    use SoftDeletes;

}



class BlogModel extends BaseModel
{

    protected $table = 'blog';
    protected $primaryKey = 'id';


    public function tags()
    {
        return $this->morphedByMany(TagsModel::class,'taggable', null, 'tag_id');
    }
}

class TagsModel extends BaseModel
{

    protected $table = 'tags';
    protected $primaryKey = 'id';

    public function blog()
    {
        return $this->morphedByMany(BlogModel::class,"taggable");
    }


}


save() 用于将带有标签的帖子保存到数据库:

  public function save($model, $title, $passage, $author, $category, $category_id, $img_url, $tag, $user_id)
    {
        $model->title = $title;
        $model->content = $passage;
        $model->author = $author;
        $model->category = $category;
        $model->category_id = $category_id;
        $model->img_url = $img_url;
        $model->user_id = $user_id;
        $model->save();
        return $model->tags()->saveMany($tag);
    }

在这里,我创建了一个标签列表,例如 ['tag1','tag2','tag3'] 并将其传递给上面介绍的 save() 方法:

    private function tags($tags)
    {
        $tag = array();
        foreach ($tags as $t)
            array_push($tag,['name' => $t]);


        return $tag;
    }

最后,错误是

Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::save() must be an instance of Illuminate\Database\Eloquent\Model, array given, called in /var/www/test/vendor/illuminate/database/Eloquent/Relations/BelongsToMany.php on line 946 and defined in /var/www/test/vendor/illuminate/database/Eloquent/Relations/BelongsToMany.php on line 927

【问题讨论】:

  • 请分享错误页面的痕迹。

标签: php mysql laravel eloquent tags


【解决方案1】:

我通过将模型更改为此解决了我的问题:


class BlogModel extends BaseModel
{

    protected $table = 'blog';
    protected $primaryKey = 'id';


    public function tags()
    {
        return $this->morphToMany(TagsModel::class,'taggable',null,null,'tag_id');
    }
}

class TagsModel extends BaseModel
{

    protected $table = 'tags';
    protected $primaryKey = 'id';

    protected $fillable = ['name'];
    public function blog()
    {
        return $this->morphedByMany(BlogModel::class,"taggable");
    }


}

我还从可标记对象中删除了主键和外键。

create table taggables
(
    tag_id        int                                 null,
    taggable_id   int                                 null,
    taggable_type varchar(512)                        null,
    created_at    timestamp default CURRENT_TIMESTAMP null,
    deleted_at    timestamp                           null,
    updated_at    timestamp                           null
);



【讨论】:

    【解决方案2】:

    你需要像这样构建标签数组:

        private function tags($tags)
        {
            $tag = array();
            foreach ($tags as $t)
                $tag[] = new TagsModel(['name' => $t]);
    
    
            return $tag;
        }
    

    【讨论】:

    • 亲爱的 Mike,这是显示给我的错误 Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`personal_web`.`taggables`, CONSTRAINT `fk23` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
    • @omid 确保您的 BlogModel 模型的可填充属性中有 tag_idlaravel.com/docs/7.x/eloquent#mass-assignment
    • 我将php protected $fillable = ['name','tag_id']; 添加到 TagModel 但没有任何改变
    猜你喜欢
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    相关资源
    最近更新 更多