1
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】入力した生年月日から年齢を抽出する

Posted at

入力した生年月日から年齢を抽出する

DBに生年月日を登録し、そのデータを使って年齢もDBに登録がしたいと考えた。

Carbonを使って実装する

$Birthday = '1994-07-02';
$years = Carbon::parse($Birthday)->age;
   
dd($years);

これを使ってLaravelで実装する。

public function update(Request $request, $id) {

    $user = User::find($id);
    $user->birthday = $request->birthday;
    $user->age = Carbon::parse($user->birthday)->age;
    $user->save();

    return redirect()->route('users.index')->with(
        'message', 'ユーザー情報を登録しました'
    );
}

parse関数の引数に生年月日を入れてあげればOK。
今回はDBに登録する$user->birthdayを使ってやってみたが成功。

以上

1
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
1
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?