Search code examples
phplaravel-5updatemodel

Update user on the same page laravel 5


I am new to the framework and I have a question. I made a login authentication to access and when I go to my profile page I can view my data and change all on the same page. I need to go get my ID to be able to do the update or not needed? What do I need to do? The way I'm doing okay? I only need to create a route::post?

my profile page:

@if(Session::has('message'))
    <div class="alert alert-danger">
    <h5>{{ Session::get('message') }}</h5>
    </div>
@endif

{!! Form::open(array('url' => 'backend/perfil', 'name' => 'updateProfile', 'role' => 'form'))!!}

<div class="row" style="margin-bottom: 20px;">
    <div class="col-md-3 col-lg-3"></div>
    <div class="col-md-2 col-lg-2">
        {!! Form::label('name', 'Utilizador', ['class' => 'label_perfil']) !!}
    </div>
    <div class="col-md-5 col-lg-5">
        {!! Form::text('name', null, ['class' => 'form-control input-md', 'placeholder' => 'Utilizador']) !!}
    </div>
</div>
<div class="row" style="margin-bottom: 20px;">
    <div class="col-md-3 col-lg-3"></div>
    <div class="col-md-2 col-lg-2">
        {!! Form::label('nascimento', 'Data de nascimento', ['class' => 'label_perfil']) !!}
    </div>
    <div class="col-md-5 col-lg-5">
        {!! Form::date('nascimento', null, ['class' => 'form-control input-md']) !!}
        {{ $errors->first('nascimento', '<span class=error>:message</span>') }}
    </div>
</div>
<div class="row" style="margin-bottom: 20px;">
    <div class="col-md-3 col-lg-3"></div>
    <div class="col-md-2 col-lg-2">
        {!! Form::label('sexo', 'Sexo', ['class' => 'label_perfil']) !!}
    </div>
    <div class="col-md-5 col-lg-5">
        {!! Form::select('sexo', ['Masculino', 'Feminino'], null, ['class' => 'form-control input-md']) !!}
    </div>
</div>
<div class="row" style="margin-bottom: 20px;">
    <div class="col-md-3 col-lg-3"></div>
    <div class="col-md-2 col-lg-2">
        {!! Form::label('email', 'Email', ['class' => 'label_perfil']) !!}
    </div>
    <div class="col-md-5 col-lg-5">
        {!! Form::text('email', null, ['class' => 'form-control input-md', 'placeholder' => 'Email']) !!}
    </div>
</div>
<div class="row" style="margin-bottom: 20px;">
    <div class="col-md-3 col-lg-3"></div>
    <div class="col-md-2 col-lg-2">
        {!! Form::label('password', 'Password', ['class' => 'label_perfil']) !!}
    </div>
    <div class="col-md-5 col-lg-5">
        {!! Form::password('password', ['class' => 'form-control input-md', 'placeholder' => 'Password']) !!}
    </div>
</div>
<div class="row" style="margin-bottom: 20px;">
    <div class="col-md-3 col-lg-3"></div>
    <div class="col-md-2 col-lg-2">
        {!! Form::label('rpassword', 'Confirmar password', ['class' => 'label_perfil']) !!}
    </div>
    <div class="col-md-5 col-lg-5">
        {!! Form::password('rpassword', ['class' => 'form-control input-md', 'placeholder' => 'Confirmar password']) !!}
    </div>
</div>                          
<div class="row" style="margin-bottom: 20px;">
    <div class="col-md-3 col-lg-3"></div>
    <div class="col-md-2 col-lg-2">
        {!! Form::label('imagem', 'Imagem', ['class' => 'label_perfil']) !!}
    </div>
    <div class="col-md-5 col-lg-5">
        {!! Form::file('imagem', ['class' => 'input-file']) !!}
    </div>
</div>   
<div class="row" style="margin-bottom: 20px; margin-top: 30px;">
    <div class="col-md-3 col-lg-3"></div>
    <div class="col-md-9 col-lg-9">
        {!! Form::submit('Enviar', ['class' => 'btn btn-primary']) !!}
    </div>
</div> 
{!! Form::close() !!}

My controller:

public function perfil() {
    return view('backend/perfil.index');        
}           

public function updateProfile() {

    $profileData = Input::except('_token');
    $validation = Validator::make($profileData, User::$profileData);
    if ($validation->passes()) {
        User::where('id', Input::get('id'))->update($profileData);
        return view('backend/perfil.index')->with('message', 'Updated Succesfully');
    } else {
        return view('backend/perfil.index')->with('message', $validation->messages());
    }
}

My route:

Route::get('backend/perfil','BackendControlador@perfil');
Route::post('backend/perfil', 'BackendControlador@updateProfile');

My app User:

public static $profileData = array(
        'email' =>  'required|email',
        'name' =>  'required',
        );

Solution

  • Here is the Detailed one what you wanted to do.

    Step 1 : Open the Form

    {!! Form::open(array('url' => 'updateProfile', 'name' => 'updateProfile', 'role' => 'form'))!!}
    

    Note : Your form method action is empty. You shall view your source to see it

    Step 2 : Write a route

    Route::post('updateProfile', 'homeController@updateProfile');

    It will call the homeController's updateProfile function

    Step 3 : Define the Controller, validate the input and make your action done via model

    Here's the simple/sample function for you

    public function updateProfile()
        {
            $profileData = Input::except('_token');
            $validation = Validator::make($profileData, User::$profileData);
            if ($validation->passes()) {
                User::where('id', Input::get('id'))->update($profileData);
                return view('profile')->with('message', 'Updated Succesfully');
            } else {
                return view('profile')->with('message', $validation->messages());
            }
    
        }
    

    What it does is it will get all the inputs except _token and store it in $profileData, then it will make a validation that is defined inside $profileData inside the User Model

    Here is the Validator, you shall modify it

    public static $profileData = array(
            'email' =>  'required|email',
            'name' =>  'required',
            );
    

    Step 4 : Return the Result

    If the validation is passed, then it will update in the table to where the user whose id is passed i.e., Input::get('id') , and we will return the page with return view('profile')->with('message', 'Updated Succesfully'); I consider your page name as profile.blade.php and you shall change it according to your blade,

    If the validation is failed, then we will return the page with error messages

    return view('profile')->with('message', $validation->messages());

    You should have this in your blade to display your error messages

    @if(Session::has('message'))
        <div class="alert alert-danger">
        <h5>{{ Session::get('message') }}</h5>
        </div>
    @endif
    

    Note :

    If you don't want to refresh the page, then you shall just do an Ajax call to pass the variables and show/hide the results that is return by the Controller

    Hope this helps you