Laravel5.8でイメージのアップロード方法でつまづいています。
現在プロフィールページを作成していて、create.blade.phpに項目を記入した後に、
index.blade.phpにリダイレクトして、プロフィール情報を表示したいと思っています。
imageのほかにはname, gender, country, bod, descriptionのカラムがあり、image以外のデータに関しては全てmysqlにデータがinsertできている状態です。
php artisan storage:linkを実行した後に、以下のコードを書きましたので、もし解決方法があればご教示ください。お願いします。
web.php
Route::prefix('user')->group(function(){ Route::resource('profile', 'UserController'); });
migration file
Schema::create('profiles', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('user_id'); $table->string('image'); $table->string('name'); $table->string('gender'); $table->string('country'); $table->string('bod'); $table->string('description'); $table->timestamps(); });
create.blade.php
<div class="image-preview" id="imagePreview"> @if(empty(Auth::user()->profile->image)) <img src="{{asset('image/image1.jpg')}}" class="image-preview__image"> @else <img src="{{asset('uploads/profile')}}/{{Auth::user()->profile->image}}" class="image-preview__image"> @endif </div> <form action="{{ route('profile.store') }}" method="POST" enctype="multipart/form-data"> @csrf <div class="preview"> <input type="file" id="file" accept="image/*" name="image"> <label for="file"> Change Image </label> </div> <ul class="information"> <li>Name :<br> <input type="text" class="name" name="name" required value="@if(!empty(Auth::user()->profile->name)){{ Auth::user()->profile->name }}@endif"> </li><br> <li>Gender :<br> <div class="gender"> <select name="gender" id="" name="gender" > <option class="option" value="@if(empty(Auth::user()->profile->gender))Select Gender @else{{ Auth::user()->profile->gender }}@endif" selected="selected" required> @if(empty(Auth::user()->profile->gender))Select Gender @else{{ Auth::user()->profile->gender }}@endif </option> <option value="male" >male</option> <option value="female" class="selected">female</option> <option value="any">any</option> </select> </div> </li> <li>Country :<br> <div class="country"> <select name="country" id="" name="country" required> <option class="option" value="@if(empty(Auth::user()->profile->country))Select country @else{{ Auth::user()->profile->country }}@endif" selected="selected"> @if(empty(Auth::user()->profile->country))Select country @else{{ Auth::user()->profile->country }}@endif </option> <option value="United Kingdom">United Kingdom</option> <option value="United States">United States</option> </select> </div> </li><br> <li>Birthday :<br> <input type="text" class="birthday" id="bod" name="bod" value="@if(!empty(Auth::user()->profile->bod)){{ Auth::user()->profile->bod }}@endif"> </li><br> <li>User Description :<br> <textarea name="description" id="" cols="60" rows="10">@if(!empty(Auth::user()->profile->description)){{ Auth::user()->profile->description }}@endif</textarea></li> </ul> <button type="submit" class="saveBtn">Save</button> </div> </form>
UserController.php
public function store(Request $request) { $this->validate($request,[ 'image'=>'image|mimes:png, jpg, jpeg|max:20000' ]); $user_id = auth()->user()->id; if($request->hasfile('image')){ $file = $request->file('image'); $ext = $file->getClientOriginalExtension(); $filename = time().'.'.$ext; $file->move('uploads/profile/', $filename); Profile::where('user_id',$user_id)->update([ 'image'=>$filename ]); } $validatedData = $request->validate([ 'name'=>'required', 'gender'=>'required', 'country' => 'required', 'bod'=>'required|before:today' ]); $user_id = auth()->user()->id; Profile::updateOrCreate( ['user_id' => $user_id], // search requirements [ 'name' => request('name'), 'gender' => request('gender'), 'country' => request('country'), 'bod' => request('bod'), 'description' => request('description') ] ); return redirect()->route('profile.index')->with('profile', Profile::all()); }
回答1件
あなたの回答
tips
プレビュー