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?

Laravel `Request` チートシート

Posted at

⚠️: 海外の記事をchatGPTで要約しただけのもの、主に自分用


💡 Laravel Request チートシート

📝 すべての入力データを取得

// 配列として取得
$input = $request->all();

// コレクションとして取得
$input = $request->collect();

🔍 特定の入力値を取得

// キーを指定して取得(デフォルト値付き)
$name = $request->input('name', 'Sally');

// 配列の入力に「ドット」記法でアクセス
$productName = $request->input('products.0.name');

// クエリ文字列の値のみ取得
$name = $request->query('name');

// JSON入力の値にアクセス
$name = $request->input('user.name');

🔠 型付きの入力値を取得

// 文字列(トリム付き)
$name = $request->string('name')->trim();

// 整数
$perPage = $request->integer('per_page');

// 真偽値(boolean)
$archived = $request->boolean('archived');

// 日付(Carbonインスタンス)
$birthday = $request->date('birthday');

// 列挙型(enum)
use App\Enums\Status;
$status = $request->enum('status', Status::class);

🏷️ 動的プロパティで入力にアクセス

$name = $request->name;

📌 入力データの一部を取得

// 指定したキーのみ取得
$credentials = $request->only(['username', 'password']);

入力の存在チェック

// 特定の入力があるか確認
if ($request->has('name')) {
    // …
}

// 入力が存在する場合にクロージャを実行
$request->whenHas('name', function (string $input) {
    // …
});

🔄 リクエストデータを追加・変更

// 追加のデータをマージ
$request->merge(['votes' => 0]);

元記事

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?