#現状
- ブログ投稿サイトを開発中。とりわけ、本件はユーザページに関する質問です。
- 各ユーザがプロフィール文('profile')とサムネイル画像('avatar')を変更することができる機能です。
- プロフィール文と画像の名前は問題なくデータベースに保存されています。
- しかし、public/images/avatarsに画像データが保存されません。
- 特にエラー文は表示されていません。
何日もハマっており、どうしても直せません。
どうかご教示して頂けたら幸いです。
宜しくお願い致します。
PS. もしかしたら、AuthControllerでアップデートアクションを定義するべきなのでしょうか? なぜなら、AuthControllerのcreateアクションでユーザの登録機能を実装しています。会員登録機能がAuthControllerなのにユーザページの編集はUsersControllerを採用しているからおかしいことになっているのでしょうか?
html
1<!-- フォーム(ユーザ編集ページのフォーム) --> 2 3<form method="POST" action="{{ url('users') . '/' . $user->id}}" accept-charset="UTF-8"><input name="_method" type="hidden" value="PATCH" enctype="multipart/form-data"> 4 <input type="hidden" name="_token" value="{{ csrf_token() }}"> 5 <input type="file" name="avatar" value="avatar"> 6 <textarea name="profile" rows="3" value="profile" style="width: 95%; margin-right: auto; margin-left: auto; margin-top: 1.5em;" placeholder="Tell me what kind of person are you...?">{{ $user->profile }}</textarea> 7 <input type="submit" value="update" style="margin: 3em 0 2em 0;"> 8 </form>
php
1// UsersControllerのupdateアクション 2 3public function update(Request $request, $id) { 4 $user = Auth::user()->id; 5 if ($request->hasFile('avatar')) { 6 $image = $request->file('avatar'); 7 $filename = time() . '.' . $image->getClientOriginalExtension(); 8 $path = public_path('images/avatars/' . $filename); 9 Image::make($image->getRealPath())->resize(400, 400)->save($path); 10 } else { 11 User::find($id)->update( 12 array( 13 'avatar' => $request->avatar, 14 'profile' => $request->profile 15 ) 16 ); 17 } 18 return view('users.update', array('user' => Auth::user())); 19 }
php
1// User.php(ユーザのモデル) 2 3<?php 4 5namespace App; 6 7use Illuminate\Foundation\Auth\User as Authenticatable; 8 9class User extends Authenticatable { 10 /** 11 * The attributes that are mass assignable. 12 * 13 * @var array 14 */ 15 protected $fillable = [ 16 'name', 'email', 'password', 'avatar', 'profile' 17 ]; 18 19 /** 20 * The attributes excluded from the model's JSON form. 21 * 22 * @var array 23 */ 24 protected $hidden = [ 25 'password', 'remember_token', 26 ]; 27 public function articles() { 28 return $this->hasMany(Article::class); 29 } 30 public function comments() { 31 return $this->hasMany(Comment::class); 32 } 33}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。