简体   繁体   中英

how to save a list of tags array in database using eloquent laravel

I am going to save a post with some tags inside my tables but I am not able to save a list of tags associated with this new post. I have a Blog, Tag, Taggable table that is presented below.

-- 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);



and this is my eloquent models for these tables:

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");
    }


}


the save() is for saving a post with its tags to the database:

  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);
    }

Here, I create a list of tags for example ['tag1','tag2','tag3'] and pass it to save() method that is presented above:

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


        return $tag;
    }

Finally, the error is

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

You need build tag array like this:

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


        return $tag;
    }

I fixed my problem by changing the model to this:


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");
    }


}

and also I remove primary and foreign key from taggables.

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
);



The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM