vendredi 6 mai 2016

Laravel 5.1 - Admin panel - Update User

i have a problem editing user registered, i need update information user but i have problem:

  • Update email user, (error email exist), i need ignore email that user editing.
  • Other information doesnt update.
  • Update password IF insrted new password.

i'm using resource controller.

UserController.php

namespace dixard\Http\Controllers\Admin;

use Illuminate\Http\Request;

use dixard\Http\Requests;
use dixard\Http\Controllers\Controller;

// colleghiamo al modello

use dixard\User;

// ci serve per validare
use Validator;

//per aggionare password
use Hash;    


public function update(Request $request, User $user)
        {

            $id= $user->id;
            // i want ignora id of user edited
            $rules = [

                'name' => 'required',

                'lastname' => 'required',

                'username' => 'required|max:50|',

                'birth' => 'required',

                'country' => 'required',

                'usertype' => 'required',

                'email' => 'required|email|unique:users'.',email,' . $id,


            ];

            $messages = [


                'name.required' => 'Campo nome richiesto',
                'lastname.required' => 'Campo cognome richiesto',
                'username.required' => 'Campo username richiesto',
                'username.max' => 'Campo massimo 50 caratteri',
                'birth.required' => 'Campo nascita richiesto',
                'country.required' => 'Campo paese richiesto',
                'usertype.required' => 'Campo tipologia richiesto',
                'email.required' => 'Campo email richiesto',
                'email.email' => 'formato email errato ',

                'email.unique' => 'Email già esistente',
                'password.min' => 'Campo password minimo 4 caratteri',
                'password.max' => 'Campo password massimo 18 caratteri',



            ];


            $validator = Validator::make($request->all(), $rules, $messages);
            if ($validator->fails()){

                return redirect('admin/user')->withErrors($validator);

            }
            // if there is not any error go to update
            else{           

                // if email id different by input, so if email input update also email
                if( $user->email != $request->email ){

                $s = new User;

                $data = array(

                     'name'             => $request->get('name'),
                'lastname'     => $request->get('lastname'),
                'username'          => $request->get('username'),
                'email'         => $request->get('email'),
                'password'      => $request->get('password'),
                'birth'         => $request->get('birth'),
                'country'       => $request->get('country'),
                'usertype'          => $request->get('usertype'),


                );

                $s->where($id, $request->get('id'))->update($data);

                return redirect('admin/user')->with('message', 'Updated successfully');
                }

                // If email input doesnt change update all ( not email)
                else{

                $s = new User;

                $data=array(

                    'name'          => $request->get('name'),
                'lastname'     => $request->get('lastname'),
                'username'          => $request->get('username'),

                'password'      => $request->get('password'),
                'birth'         => $request->get('birth'),
                'country'       => $request->get('country'),
                'usertype'          => $request->get('usertype'),


                     );

                $s->where($id, '=', $request->get('id'))->update($data);

                return redirect('admin/user')->with('message-error', 'error');


                }

            }
        }

ROUTES.PHP

Route::resource('admin/user','Admin\UserController');



Route::bind('user', function($user){

    return dixard\User::find($user);
});

model USER.PHP

    protected $table = 'users';


    protected $fillable = [

    'name',
    'username',
    'lastname',
    'birth',
    'profile',
    'country',
    'usertype',
    'email',
    'password',
    'social',
    'confirm_token',
    'active',

    ];

    // Ogni utente HA tanti prodotti.

    public function products() 
    {

        return $this->hasMany('dixard\Product');    

    }


    protected $hidden = ['password', 'remember_token'];

    // setare la password ogni volta che viene cambiata

    public function setPasswordAttribute($valor){
        //se non è vuota viene applicato hash
       if(!empty($valor)) {

        $this->attributes['password']= \Hash::make($valor);   
       }


    }

EDIT.PHP view page

{!! Form::open(['route'=>'admin.user.update', 'class'=>'form-horizontal form-label-left']

                )!!}


                <input type="hidden" name="_method" value="PUT">


                  <div class="form-group">
                    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Nome<span class="required">*</span>
                    </label>
                    <div class="col-md-6 col-sm-6 col-xs-12">
                      <input type="text"  id="name"  name="name" class="form-control col-md-7 col-xs-12" value="">
                    </div>
                  </div>


                  <div class="form-group">
                    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="lastname">Cognome<span class="required">*</span>
                    </label>
                    <div class="col-md-6 col-sm-6 col-xs-12">
                      <input type="text" id="lastname"  name="lastname" class="form-control col-md-7 col-xs-12" value="">
                    </div>
                  </div>

                  .... So for all fields inputs


                      <a href="" class="btn btn-warning">cancel</a>
                      <button type="submit" class="btn btn-success">update</button>
                    </div>
                  </div>


                {!! Form::close()!!}

Thank you for your help!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire