実現したいこと
Laravelでアイテム(画像付き)の登録、修正、削除機能を実装したい。
前提
Laravel10
DBにパスを保存し、画像はファイルで保存する。
発生している問題・エラーメッセージ
修正時に入力時のパスを表示するためにローカルパスを保存しようと思います。
bootstrapのファイル選択を使用していますが、入力したパスを取得できません。
<input type="file" class="form-control custom-file-input" name="path1" value="{{ old('path1') }}">
このnameをそのままDBに登録すると、/tmp/phpNUbiIN みたいなパスになります。
(本当はダウンロード/tea1.jpg)
また、edit画面で入力項目にセットしようとしますが、上記のようなパスなせいか、値が表示されません。
該当のソースコード
create.blade.php
1<form method="post" action="{{ route('item.store') }}" enctype="multipart/form-data"> 2 @csrf 3 4 <input tyepe="text" class="form-control" name="item_name" placeholder="ItemName" value="{{ old('item_name') }}"> 5 @error('item_name') 6 <div class='error'>{{ $message }}</div> 7 @enderror 8 <input tyepe="number" class="form-control" name="price" placeholder="Price" value="{{ old('price') }}"> 9 @error('price') 10 <div class='error'>{{ $message }}</div> 11 @enderror 12 <textarea class="form-control" name="description" cols="30" rows="10" placeholder="description">{{ old('description') }}</textarea> 13 @error('description') 14 <div class='error'>{{ $message }}</div> 15 @enderror 16 <input type="file" class="form-control custom-file-input" name="local_path1" value="{{ old('path1') }}"> 17 @error('path1') 18 <div class='error'>{{ $message }}</div> 19 @enderror 20 <input type="file" class="form-control custom-file-input" name="local_path2" value="{{ old('local_path2') }}"> 21 <input type="file" class="form-control custom-file-input" name="local_path3" value="{{ old('local_path3') }}"> 22 <input type="file" class="form-control custom-file-input" name="local_path4" value="{{ old('local_path4') }}"> 23 <input type="file" class="form-control custom-file-input" name="local_path5" value="{{ old('local_path5') }}"> 24 <input type="file" class="form-control custom-file-input" name="local_path6" value="{{ old('local_path6') }}"> 25 26 <button class="btn btn-primary mt-4 fs-4">登録</button> 27 </form>
ItemController
1public function edit(Item $item) 2 { 3 return view('tea.item.edit') 4 ->with(['item' => $item]); 5 }
edit.blade.php
1<form method="post" action="" enctype="multipart/form-data"> 2 @method('PATCH') 3 @csrf 4 5 <input tyepe="text" class="form-control" name="item_name" placeholder="ItemName" value="{{ old('item_name', $item->item_name) }}"> 6 @error('item_name') 7 <div class='error'>{{ $message }}</div> 8 @enderror 9 <input tyepe="number" class="form-control" name="price" placeholder="Price" value="{{ old('price', $item->price) }}"> 10 @error('price') 11 <div class='error'>{{ $message }}</div> 12 @enderror 13 <textarea class="form-control" name="description" cols="30" rows="10" placeholder="description">{{ old('description', $item->description) }}</textarea> 14 @error('description') 15 <div class='error'>{{ $message }}</div> 16 @enderror 17 <input type="file" class="form-control custom-file-input" name="local_path1" value="{{ old('local_path1', $item->path1) }}"> 18 @error('local_path1') 19 <div class='error'>{{ $message }}</div> 20 @enderror 21 <input type="file" class="form-control custom-file-input" name="local_path2" value="{{ old('local_path2', $item->local_path2) }}"> 22 <input type="file" class="form-control custom-file-input" name="local_path3" value="{{ old('local_path3', $item->local_path3) }}"> 23 <input type="file" class="form-control custom-file-input" name="local_path4" value="{{ old('local_path4', $item->local_path4) }}"> 24 <input type="file" class="form-control custom-file-input" name="local_path5" value="{{ old('local_path5', $item->local_path5) }}"> 25 <input type="file" class="form-control custom-file-input" name="local_path6" value="{{ old('local_path6', $item->local_path6) }}"> 26 27 <button class="btn btn-primary mt-4 fs-4">修正</button> 28 </form>
試したこと
edit.blade.phpの中でDDしてみましたが、ローカルパスは当然「/tmp/phpNUbiIN」と表示されました。
拙いつまづきかと思いますが、よろしくお願いいたします。
追記1)
store
1public function store(ItemRequest $request) 2 { 3 $item = new Item(); 4 $item->item_name = $request->item_name; 5 $item->price = $request->price; 6 $item->description = $request->description; 7 8 $item->local_path1 = $request->local_path1; 9 if ($request->hasFile('local_path1')) { 10 $image_path = $request->file('local_path1')->store('public/tea/'); 11 $item->uploaded_path1 = basename($image_path); 12 } 13 $item->local_path2 = $request->local_path2; 14 if ($request->hasFile('local_path2')) { 15 $image_path = $request->file('local_path2')->store('public/tea/'); 16 $item->uploaded_path2 = basename($image_path); 17 } 18 $item->local_path3 = $request->local_path3; 19 if ($request->hasFile('local_path3')) { 20 $image_path = $request->file('local_path3')->store('public/tea/'); 21 $item->uploaded_path3 = basename($image_path); 22 } 23 $item->local_path4 = $request->local_path4; 24 if ($request->hasFile('local_path4')) { 25 $image_path = $request->file('local_path4')->store('public/tea/'); 26 $item->uploaded_path4 = basename($image_path); 27 } 28 $item->local_path5 = $request->local_path5; 29 if ($request->hasFile('local_path5')) { 30 $image_path = $request->file('local_path5')->store('public/tea/'); 31 $item->uploaded_path5 = basename($image_path); 32 } 33 $item->local_path6 = $request->local_path6; 34 if ($request->hasFile('local_path6')) { 35 $image_path = $request->file('local_path6')->store('public/tea/'); 36 $item->uploaded_path6 = basename($image_path); 37 } 38 39 $item->save(); 40 41 return redirect() 42 ->route('item.index'); 43 }

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/04/23 23:59
2023/04/24 00:23 編集