LoginSignup
2
4

More than 1 year has passed since last update.

laravel-adminでcheckboxをDBに保存

Last updated at Posted at 2020-01-07

larave-adminはお手軽にさらっと管理画面を作れて便利です。
大まかな使用方法は他の記事にお任せするとしてcheckboxmultiselectなどをDBに保存するにはひと手間必要なので備忘録的メモです

公式に記述があります。二つあるようですが今回はデータをカンマ区切りで登録する方法です。
https://laravel-admin.org/docs/#/en/model-form-fields

class Post extends Model

    public function getTagsAttribute($value)
    {
        return explode(',', $value);
    }

    public function setTagsAttribute($value)
    {
        $this->attributes['tags'] = implode(',', $value);
    }
}

そのまま引用しています。get[elements]Attributeset[elements]Attributeをoverwriteしています。

マイグレーションはtextかvarcharあたりで

            Schema::create('post', function (Blueprint $table) {
                $table->bigIncrements('id');

                $table->text('tag');
                $table->timestamps();
                $table->softDeletes();

            });

これでDBにはカンマ区切りで保存されます

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4