2016-09-25 6 views
-2

私のユーザーのアバター画像の更新に関して少し問題があります。更新ユーザーアバタープロフィールLaravel 4.2

Imageの多形関連テーブルがあります。ユーザープロファイルの情報を更新し、自分のDBに新しいアバターをアップロードすると、新しいエントリが作成され、自分のテーブルイメージの現在のIDは更新されません。

表画像同上|パス| Imageable_id | imageable_type |のcreated_at

私がUserController方法更新

public function update($id){ 

    $rules =[ 
     /*'lastname' => 'min:3|string', 
     'firstname' => 'min:3|string', 
     'username'=> 'min:4|unique:users', 
     'mail' => ' email|unique:users', 
     'birthday' => 'date_format:d-m-Y|before:today', 
     'country'=>'min:3', 
     'type_street'=>'min:3', 
     'number'=>'min:1|numeric', 
     'street'=>'min:4|string', 
     'complementary_street'=>'min:2|string', 
     'town'=>'min:2|string', 
     'zip'=>'min:4|numeric', 
     'phone_home'=>'min:10|numeric', 
     'phone_mobile'=>'min:10|numeric', 
     'image_path'=>'image|max:1000|mimes:jpeg,jpg,png',*/ 
    ]; 

    $validator = Validator::make(Input::all(),$rules); 

    if($validator->fails()){ 
     return Redirect::to('/profil/'.$id) 
      ->with('alert_error','Merci de corriger les erreurs'); 

    }else{ 
     $user = User::find($id); 

     $user->lastname  = Input::get('lastname'); 
     $user->firstname = Input::get('firstname'); 
     $user->username  = Input::get('username'); 
     $user->mail   = Input::get('mail'); 
     $user->birthday  = Input::get('birthday'); 
     $user->adresse->type_street = Input::get('type_street'); 
     $user->adresse->number  = Input::get('number'); 
     $user->adresse->street  = Input::get('street'); 
     $user->adresse->complementary_street  = Input::get('complementary_street'); 
     $user->adresse->town   = Input::get('town'); 
     $user->adresse->zip   = Input::get('zip'); 
     $user->adresse->country  = Input::get('country'); 
     $user->adresse->phone_home  = Input::get('phone_home'); 
     $user->adresse->phone_mobile  = Input::get('phone_mobile'); 

     if(Input::hasFile('avatar')){ 
      $avatar = Image::find($id); 

      $file = Input::file('avatar'); 
      $name = time().'-'.$file->getClientOriginalName(); 

      $file = $file->move('img/avatar/', $name); 
      $input['path'] = 'img/avatar/'.$name; 
      $input['imageable_id'] = $user->id; 
      $input['imageable_type'] = 'User'; 

      $avatar = new Image($input); 
      $avatar->save(); 
     } 

     $user->adresse->save(); 

     return Redirect::to('/profil/'.$id) 
      ->with('alert_success','Modification sauvegardé avec succès'); 

    } 
} 

は、あなたがこの機能のために私を助けることができる何が私の電流IDの更新理由を私は理解していません。新しいOneを作成します。

感謝の

答えて

0

あなたは私のアバターを保存するので、どのように$avatar = new Image($input);

+0

OK新しいインスタンスを使用して検索$avatar = Image::find($id);を上書きしていますか? – nicolassiuol