uknowcreation's avatar

Image upload Polymorphics relation

Hello,

I have a little problem, i need to update avatar of user in my profil view.

For this i use polymorphic relation with an Images table, -id -user_id -path -imageable_id -imageable_type

I need to save the record (update) in this table.

My model Image public function imageable(){

    return $this->morphTo();;
}

My Model User public function defaultImage(){ return $this->image()->first(); }

In my users controller update method i have the upload method working fine but not saving the record in Images table (updating)

public function update($id){

    $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')){

            $image = Input::file('avatar');

            //avoir le chemin original de la photo avec le nom de départ
            $filename  = time() . '.' . $image->getClientOriginalExtension();

            $path = public_path('img/avatar/' . $filename);
            Image::make($image->getRealPath())->resize(468, 249)->save($path);
            $user->image->path = 'img/products/'.$filename;
            $user->defaultImage()->save();
        }
        $user->adresse->save();

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


    }


}

Can you help me for this little problem

Thanks

0 likes
0 replies

Please or to participate in this conversation.