実現したいこと
ajaxでinputタグをクリックしたら、項目がでてきて複数選択をすることが
できるのですが、これをNullでも登録できるようにしたい
前提
情報を登録しようとしているのですが、
配列で複数のオプションのある項目をNullにすると
エラーが発生してしまいます。
Nullで登録できる方法をご教授いただけますと幸いです
※情報を入力して登録すると、問題なく保存をすることができます。
発生している問題・エラーメッセージ
implode(): Argument #1 ($pieces) must be of type array, string given
該当のソースコード
viewファイル
<div class="col-sm-4"> <div class="mb-3"> <label class="form-label">Property Amenities </label> <select name="amenities_id[]" class="js-example-basic-multiple form-select" multiple="multiple" data-width="100%"> @foreach($amenities as $ameni) <option value="{{ $ameni->id }}">{{ $ameni->amenitis_name }}</option> @endforeach </select> </div> </div><!-- Col -->
コントローラーファイル
public function StoreProperty(Request $request) { $amen = $request->amenities_id; $amenites = implode('`, `', $amen); // フォームからきた画像を受け取る $image = $request->file('property_thambnail'); // uniqidを10進数にし、文字列連携で、 // $imageの拡張子(png)を取得しファイル名作成 $name_gen = hexdec(uniqid()) . '.' . $image->getClientOriginalExtension(); Image::make($image)->resize(370, 250) ->save('upload/property/thambnail/' . $name_gen); // 保存処理をするため、変数($save_url)作成 $save_url = 'upload/property/thambnail/' . $name_gen; $pcode = IdGenerator::generate([ 'table' => 'properties', 'field' => 'property_code', 'length' => 5, 'prefix' => 'PC' // PC:property codeの略 ]); // insertGetIdで追加したID情報を取得 $property_id = Property::insertGetId([ 'ptype_id' => $request->ptype_id, 'amenities_id' => $amenites, 'property_name' => $request->property_name, // strtolowerで大文字を小文字に変換 // フォームから来たproperty_nameに空白があった場合、 // -(ハイフン)に置換する 'property_slug' => strtolower(str_replace(' ', '-', $request->property_name)), 'property_code' => $pcode, 'property_status' => $request->property_status, 'lowest_price' => $request->lowest_price, 'max_price' => $request->max_price, 'short_descp' => $request->short_descp, 'long_descp' => $request->long_descp, 'bedrooms' => $request->bedrooms, 'bathrooms' => $request->bathrooms, 'garage' => $request->garage, 'garage_size' => $request->garage_size, 'property_size' => $request->property_size, 'property_video' => $request->property_video, 'address' => $request->address, 'city' => $request->city, 'state' => $request->state, 'postal_code' => $request->postal_code, 'neighborhood' => $request->neighborhood, 'latitude' => $request->latitude, 'longitude' => $request->longitude, 'featured' => $request->featured, 'hot' => $request->hot, 'agent_id' => $request->agent_id, 'status' => 1, 'property_thambnail' => $save_url, // Carbonで現在の時間を取得 'created_at' => Carbon::now(), ]);
試したこと
validate()でnullを許容しようとしましたが、効果がありませんでした
補足情報(FW/ツールのバージョンなど)
Laravel10
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー