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 とPHP TipsAdvent Calendar 2023

Day 15

Requestオブジェクトから値を取り出した値をキャストしていませんか?

Posted at

概要

requestオブジェクトから値を取り出す際にキャストした状態で値を取り出す事ができます。

public function store(Request $request)
{
    // bool型にキャストして取り出し
    $request->boolen('is_active');

    // string型にキャストして取り出し
    $request->string('name');

    //Carbon型にキャストして取り出し
    $request->date('start_date');

    //int型にキャストして取り出し
    $request->integer('number');

    //enumにマッチした値を取り出し
    $request->enum('status', Status::class);
}

参考

bool型キャスト

string型キャスト

Carbon型キャスト

enum型キャスト

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?