実現したいこと
ユーザーがチェックボックスにチェックを入れたら
"""alway_delivery_flg"""に1をたてたい
何も入れなかったら0にしたい。
発生している問題・分からないこと
チェックボックスをチェックしている状態だとDBに保存されるが
チェックボックスをチェックしていない状態だと上記のエラーが表示される。
エラーメッセージ
error
1Illuminate 2 \ 3Database 4 \ 5QueryException 6PHP 8.1.0 79.52.16 8SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'alway_delivery_flg' cannot be null 9INSERT INTO 10 `curriculums` ( 11 `title`, 12 `description`, 13 `video_url`, 14 `alway_delivery_flg`, 15 `grade_id`, 16 `updated_at`, 17 `created_at` 18 ) 19VALUES 20 ( 21 ss, 22 eee, 23 eee, 24 ?, 25 1, 26 2024 -05 -31 02: 06: 20, 27 2024 -05 -31 02: 06: 20 28 )
該当のソースコード
php
1 public function storeCurriculum($grade , $request) 2 3 { 4 5 //授業データを保存 6 7 $curriculums = new Curriculum(); 8 9 $curriculums->title = $request->input('title'); 10 11 12 13 //ここにサムネイル画像を入れる 14 15 // 商品画像を保存 16 17 if ($request->hasFile('thumbnail_image')) { 18 19 $imagePath = $request->file('thumbnail_image')->store('images', 'public'); 20 21 $curriculums->thumbnail = $imagePath; 22 23 } 24 25 26 27 $curriculums->description = $request->input('description'); 28 29 $curriculums->video_url = $request->input('video_url'); 30 31 $curriculums->alway_delivery_flg = $request->input('alway_delivery_flg'); 32 33 $curriculums->grade_id = $request->input('grade_id'); 34 35 36 37 $curriculums->save(); 38 39 return $curriculums; 40 41 }
php
1<label for="title">授業名:</label> 2 <input type="text" id="title" name="title"> 3 <label for="video_url">動画URL:</label> 4 <input type="text" id="video_url" name="video_url"> 5 <label for="description">授業概要:</label> 6 <textarea id="description" name="description" rows="4" cols="50"></textarea> 7 <label for="alway_delivery_flg">常時公開:</label> 8 <input type="checkbox" id="alway_delivery_flg" name="alway_delivery_flg" value="1"> 9 <input type="submit" value="登録">
php
1public function rules() 2 { 3 return [ 4 'title' =>'required|max:255', 5 'description'=>'required|max:2000', 6 'thumbnail' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048', 7 'image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048', 8 ]; 9 } 10 public function messages() 11 { 12 return [ 13 'title.required' => '授業名は必須項目です。', 14 'title.max' => '授業名は255文字以内で入力してください。', 15 'description.required' => '授業概要は必須項目です。', 16 'description.max' => '授業概要は2000文字以内で入力してください。', 17 'thumbnail.image' => '商品画像は画像ファイルを選択してください。', 18 'thumbnail.mimes' => '商品画像はjpeg、png、jpg、gif形式の画像ファイルを選択してください。', 19 'thumbnail.max' => '商品画像のサイズは2MB以下にしてください。', 20 ]; 21 } 22}
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
チャットGPTからヒントをもらい試してみたが、
試した結果
チェックボックスをチェックしている状態だとDBに保存される。
チェックボックスをチェックしていない状態だと保存されず、何も起こらない。
下記チャットGPTの文
補足
public function rules()
{
return [
'company_id' => 'required|integer',
'product_name' => 'required|string|max:255',
'price' => 'required|numeric',
'stock' => 'required|integer',
'comment' => 'nullable|string',
'img_path' => 'nullable|string',
'alway_delivery_flg' => 'required|boolean',
];
}

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。