简体   繁体   中英

can't update data in laravel 5.2

i have 2 database : 1. users (name, username, password, remember_token, admin) 2. dosen (iddosen, user_id, namadosen, nipy, etc)

i want to update data but when i click save it's not updated to database. there's no error when i click save.

in edit view i use relationship
{!! Form::model($user->dosen, ['route' => ['admin.dosen.update', $user->dosen->user_id], 'method' => 'PUT']) !!}

this is my method :

public function update($id)
{
    $userUpdate = Request::all();
    $user = User::find($id);
    $user->update($userUpdate);
    return redirect('admin/dosen')->with('message', 'Data berhasil diubah!');
}

Edit view:

@extends('layouts.app')
@section('content')

<div class="container">
<div class="row">
    <div class="col-md-10 col-md-offset-1">
        <div class="panel panel-default">
            <div class="panel-heading">Edit {{ $user->dosen->namadosen }}</div>

            <div class="panel-body">
                <!-- jika terjadi error, akan menampilkan pesan -->
                        @if ($errors->any())
                            <ul class="alert alert-danger">
                            @foreach ($errors->all() as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                            </ul>
                        @endif

                        {!! Form::model($user->dosen, ['route' => ['admin.dosen.update', $user->dosen->user_id], 'method' => 'PUT']) !!}

                            <div class="form-group">
                                {!! Form::label('iddosen', 'Kode Dosen') !!}
                                {!! Form::text('iddosen', null, ['class' => 'form-control', 'readonly' => 'true']) !!}
                            </div>

                            <div class="form-group">
                                {!! Form::label('nipy', 'NIPY') !!}<br>
                                {!! Form::text('nipy', null, ['class' => 'form-control']) !!}
                            </div>

                            <div class="form-group">
                                {!! Form::label('namadosen', 'Nama Dosen') !!}
                                {!! Form::text('namadosen', null, ['class' => 'form-control']) !!}
                            </div>

                            <div class="form-group">
                                {!! Form::label('alamatdosen', 'Alamat') !!}
                                {!! Form::textarea('alamatdosen', null, ['class' => 'form-control']) !!}
                            </div>

                             <div class="form-group">
                                {!! Form::label('notelpdosen', 'No HP Dosen') !!}
                                {!! Form::text('notelpdosen', null, ['class' => 'form-control']) !!}
                            </div>

                             <div class="form-group">
                                {!! Form::label('tempatlahirdosen', 'Tempat & Tanggal Lahir') !!}
                                <div class="form-inline">
                                {!! Form::text('tempatlahirdosen', null, ['class' => 'form-control']) !!}
                                {!! Form::text('tanggallahirdosen', null, ['class' => 'form-control']) !!}
                            </div>
                            </div>



                            {{ Form::button('<i class="fa fa-check-square-o"></i> Simpan', ['type' => 'submit', 'class' => 'btn btn-primary'] )  }}
                            <a class="btn btn-small btn-success" href="{{ URL('dosen/') }}"><i class="fa fa-reply"></i> Kembali</a>

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

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
@endsection

From the form it looks like you want to update the dosen table, not the user one. You have to explicitly do that. $user->update() will only update your name, username, password fields.

So rather return the relation and then update it.

public function update($id)
{
    $dosenUpdate = Request::all();
    $user = User::find($id);
    $user->dosen()->update($dosenUpdate);
    return redirect('admin/dosen')->with('message', 'Data berhasil diubah!');
}

It looks like you are missing updated_at column in user table. So let try to add updated_at column with "DATETIME" datatype and test it again.

You also should check messages in error log file of your application. It is usually configured in your virtual host or it is the default error log file of Apache.

Beside that you should check if you already enabled debug mode in config/app.php to see error messages.

'debug' => env('APP_DEBUG', true),

If so, error messages will be stored in storage/logs/laravel.log.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM