0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

laravel で before_save , after_save

Posted at

DBに保存する前に、初期値を入れたい。
cakephp なら before_save , after_save と言われていた。

参考
https://qiita.com/b_a_a_d_o/items/ca06024d80499f1ece14

方法

新規保存時に、位置情報がなければデフォルトで位置情報をセットして保存。

user.php

//    フックメソッド
    protected static function boot()
    {
        parent::boot();

        self::creating(function($model){

            if (!isset($model->location)){
                $model->location = \DB::raw("GeomFromText('POINT(0 0)')");
            }

            if (!isset($model->station_location)){
                $model->station_location = \DB::raw("GeomFromText('POINT(0 0)')");
            }

        });

    }

phpmyadminだと位置情報の初期値を入れることができず、
新規保存する際に位置情報が無いとエラーが出る。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?