前提
LaravelでCRUDのできるWEBアプリを作っています。
実現したいこと
現在、画像アップロード機能を実装し終えて、画像を更新する(Update)機能を実装したいです。
しかし、以下のようなエラーが発生してしまい進めません。
調べましたが出てきませんでした。
発生している問題・エラーメッセージ
Call to a member function store() on string
該当のソースコード
edit.blade.php
1<form method="POST" action="{{ route('posts.update', [$post->id]) }}" enctype="multipart/form-data" id="form"> 2 @csrf 3 @method('PUT') 4 5 <div class="form-group row align-items-center mb-2"> 6 <label for="image" class="col-md-4 col-form-label text-md-right">{{ __('Image') }}</label> 7 8 <div class="col-md-6 d-flex align-items-center"> 9 <img src="/storage/{{$post->image}}" class="mr-2" width="100" alt="image"> 10 <input type="file" accept=".png, .jpg, .jpeg" name="image" class="form-control @error('image') is-invalid @enderror" value="{{ asset('/storage/' .$post->image) }}" autocomplete="image"> 11 <br> 12 @error('image') 13 <span class="invalid-feedback" role="alert"> 14 <strong>{{ $message }}</strong> 15 </span> 16 @enderror 17 </div> 18 </div> 19 20以下略
PostsController.php
1public function update(PostRequest $request, Post $post) 2{ 3 { 4 $data = $request->all(); 5 $post->updatePost($data); 6 7 return redirect('users'); 8 } 9}
Post.php
1public function updatePost(Array $data) 2 { 3 if (isset($data['image'])) { 4 dd($data) 5 $file_name = $data['image']->store('public/'); 6 7 $this::where('id', $this->id) 8 ->update([ 9 'image' => basename($file_name), 10 'type' => $data['type'], 11 'time' => $data['time'], 12 'price' => $data['price'], 13 'how_use' => $data['how_use'] 14 ]); 15 } else { 16 $this::where('id', $this->id) 17 ->update([ 18 'type' => $data['type'], 19 'time' => $data['time'], 20 'price' => $data['price'], 21 'how_use' => $data['how_use'] 22 ]); 23 } 24 25 return; 26 }
dd($data)
の中身は以下です。
^ Illuminate\Http\UploadedFile {#1296 ▼ -test: false -originalName: "bsk.jpeg" -mimeType: "image/jpeg" -error: 0 #hashName: null path: "/tmp" filename: "phpohha5t" basename: "phpohha5t" pathname: "/tmp/phpohha5t" extension: "" realPath: "/tmp/phpohha5t" aTime: 2022-06-22 11:52:52 mTime: 2022-06-22 11:52:52 cTime: 2022-06-22 11:52:52 inode: 274324 size: 142267 perms: 0100600 owner: 33 group: 33 type: "file" writable: true readable: true executable: false file: true dir: false link: false }
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/06/22 02:55
2022/06/22 02:56