前提
LaravelでCRUDのできるWEBアプリを作っています。
実現したいこと
現在、画像アップロード機能を実装し終えて、画像を更新する(Update)機能を実装したいです。
しかし、以下のようなエラーが発生してしまい進めません。
調べましたが出てきませんでした。
発生している問題・エラーメッセージ
Call to a member function store() on string
該当のソースコード
edit.blade.php
<form method="POST" action="{{ route('posts.update', [$post->id]) }}" enctype="multipart/form-data" id="form"> @csrf @method('PUT') <div class="form-group row align-items-center mb-2"> <label for="image" class="col-md-4 col-form-label text-md-right">{{ __('Image') }}</label> <div class="col-md-6 d-flex align-items-center"> <img src="/storage/{{$post->image}}" class="mr-2" width="100" alt="image"> <input type="file" accept=".png, .jpg, .jpeg" name="image" class="form-control @error('image') is-invalid @enderror" value="{{ asset('/storage/' .$post->image) }}" autocomplete="image"> <br> @error('image') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> 以下略
PostsController.php
public function update(PostRequest $request, Post $post) { { $data = $request->all(); $post->updatePost($data); return redirect('users'); } }
Post.php
public function updatePost(Array $data) { if (isset($data['image'])) { dd($data) $file_name = $data['image']->store('public/'); $this::where('id', $this->id) ->update([ 'image' => basename($file_name), 'type' => $data['type'], 'time' => $data['time'], 'price' => $data['price'], 'how_use' => $data['how_use'] ]); } else { $this::where('id', $this->id) ->update([ 'type' => $data['type'], 'time' => $data['time'], 'price' => $data['price'], 'how_use' => $data['how_use'] ]); } return; }
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 }
よろしくお願いします。
まだ回答がついていません
会員登録して回答してみよう