Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Christianus's avatar

Update Form Data Laravel 5.2

Hello guys today i want to ask you about retrieving and update data in database

first i'll show you my code

This is my blade.php (view)

    <form class="form-horizontal" role="form" method="POST" action="{{ url('/keluhan') }}">
                        {{ csrf_field() }}  
                        <div class="form-group">
                            <label for="area" class="col-md-4 control-label">Pilih Unit</label>
                            <div class="col-md-6">
                                <select name="area">
                                    <option value="Quality Control">Quality Control</option>
                                    <option value="Customer Service">Customer Service</option>
                                    <option value="PPIC">PPIC</option>
                                    <option value="Produksi 1">Produksi 1</option>
                                    <option value="Produksi 2">Produksi 2</option>
                                    <option value="Finishing">Finishing</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="area" class="col-md-4 control-label">Jenis Keluhan</label>
                            <div class="col-md-6">
                                <select name="area">
                                    <option value="Reject">Komplain</option>
                                    <option value="Complain">Reject</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="area" class="col-md-4 control-label">Status Keluhan</label>
                            <div class="col-md-6">
                                <select name="area">
                                    <option value="Pending">Pending</option>
                                    <option value="Proses">Proses</option>
                                    <option value="Selesai">Selesai</option>
                                </select>
                            </div>
                        </div>

                        <div>
                            <label for="produk" class="col-md-4 control-label" name="produk">Nama Produk</label>

                            <div class="col-md-6">
                                <input id="produk" type="text" class="form-control" name="produk" required="true">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="area" class="col-md-4 control-label">Pilih Area</label>
                            <div class="col-md-6">
                                <select name="area">
                                    <option value="Jawa Barat">Jawa Barat</option>
                                    <option value="Jawa Tengah">Jawa Tegah</option>
                                    <option value="Jawa Timur">Jawa Timur</option>
                                    <option value="Luar Area Jawa">Luar Area Jawa</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="kategori" class="col-md-4 control-label">Kategori</label>
                            <div class="col-md-6">
                                <select name="kategori">
                                    <option value="Keluhan Pelanggan">Keluhan Pelanggan</option>
                                    <option value="Survey Pelanggan">Survey Pelanggan</option>
                                    <option value="Inspeksi">Inspeksi</option>
                                    <option value="Audit">Audit</option>
                                    <option value="Gagal Proses">Gagal Proses</option>
                                    <option value="Analisa Data">Analisa Data</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="masalah" class="col-md-4 control-label" required="true">Keluhan Pelanggan</label>
                            <textarea style="resize: none; height: 100px" name="masalah"></textarea>
                        </div>

                        <div class="form-group">
                            <label for="keterangan" class="col-md-4 control-label">Keterangan Tambahan</label>
                            <textarea style="resize: none; height: 100px" name="keterangan"></textarea><p>
                        </div>


                        <div align="center">
                            <div>
                                <h7><b>Tambahkan Gambar</b></h7>
                                <h7>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</h7>
                                <h7><b>Disetujui Oleh (PuraBox)</b></h7>
                            </div>
                            <div>
                                <input class="field" name="gambarkeluhan" type="file" required="true">
                                <input class="field" name="ttdcus2" type="file" required="true">
                                <br>
                                <br>
                                <br>
                                <br><br>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4" align="center">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fa fa-btn fa-sign-in"></i> Update
                                </button>                            
                            </div> 
                        </div>

and this is my Route

Route::get('editkeluhan/{id}','AdminController@editkeluhan');
Route::patch('editskeluhan/{id}', 'AdminController@updatekeluhannadmin');

And this is the Controller

public function editkeluhan($id){
        $halaman="tindaklayanan";
        $keluhan=keluhan::findOrFail($id);
        return view('layanankonsumen.editkeluhan',compact('keluhan','halaman'));
    }

    public function updatekeluhanadmin($id){
        $halaman = 'tindaklayanan';
        $keluhan=keluhan::findOrFail($id);
        $keluhan->produk=$request->produk;
        $sasaran->save();
        return redirect('/');
    }

The Question :

  1. First when the form opened (before click button Update) i want to retrieving all the data store before in database to each field that i want to update
  2. Second i want to update the value after click the button, how to do that ? please repair my AdminController
0 likes
62 replies
ankitparmar372's avatar
public function updatekeluhanadmin($id , Request $request){
    $halaman = 'tindaklayanan';
    $keluhan=keluhan::findOrFail($id);
    $keluhan->produk=$request->produk;
    $sasaran->save();
    return redirect('/');
}

and include this line if not exist after namespace use Illuminate\Http\Request;

Christianus's avatar

@ankitparmar372 i've but its end with error like this bro : No query results for model [App\Http\Models\keluhan].

what should i do now ? i've use App\Http\Models\keluhan; too

tisuchi's avatar

You may update @ankitparmar372 answer bit-

public function updatekeluhanadmin($id , Request $request){
    $halaman = 'tindaklayanan';
    $keluhan=keluhan::findOrFail($id);
    
    //check whether its return anything or not
    if($keluhan){
        $keluhan->produk=$request->produk;
        $sasaran->save();
    }

    return redirect('/');
}
2 likes
Christianus's avatar

mr @tisuchi i found that the problem is on findOrFail($id) but i can't find a way to repair it :(

tisuchi's avatar

What problem is that than?

If findOrFail() not working, may be you can try by simply find() or where() method also.

1 like
Snapey's avatar

Two things are going to important to you.

  1. tell Laravel that the form is a patch request by including {{ method_field('PATCH') }} somewhere in your form

  2. use old() to insert data from the previous save or data from the model https://laravel.com/docs/5.4/helpers#method-old

    <input id="produk" type="text" class="form-control" name="produk" required="true" value={{ old('produk', $keluhan->produk) }}>

Also, check this thread https://laracasts.com/discuss/channels/laravel/old-data-didnt-display-in-edit-page

1 like
Christianus's avatar

im not sure what is the problem but the code is not working when it passing id, please look this @Snapey @tisuchi

So this is My View Form... I've try to show id(berfore i passing it again) & the result is yes it shown on this page

form class="form-horizontal" role="form" method="POST" action="{{ url('/editkeluhan/{id}') }}" enctype="multipart/form-data">
                        {{ csrf_field() }}

    <div>
        <input name="id" value="{{$keluhan->id}}"> //<----- Will be hidden
    <div>

<div>
    <label for="produk" class="col-md-4 control-label" name="produk">Nama Produk</label>

    <div class="col-md-6">
        <input id="produk" type="text" class="form-control" name="produk" value="{{$keluhan->produk}}" required="true">
    </div>
</div>

now we go to our Route first (idk if there's something wrong MAYBE method post must be changed into patch ? but its still not work too)

Route::post('editkeluhanadmin/{id}', 'AdminController@updatekeluhanadmin');

Okay last is my Controller

    public function updatekeluhanadmin($id,Request $request){
        $halaman = 'tindaklayanan';
        $keluhan=keluhan::findOrFail($id); //<--- I think fail found the id
        $keluhan->produk=$request->produk;
        $sasaran->save();
        return redirect('/');
    }
Christianus's avatar

please check the code mr @Snapey mr @tisuchi is there something wrong, or miss, or must be replace ? i've browse all the day, try one by one, and still not found the solution for myself, now the error is said MethodNotAllowedHttpException

tisuchi's avatar

In this case, can you dd() your result to check that whether its working or not?

public function updatekeluhanadmin($id , Request $request){
    $halaman = 'tindaklayanan';
    $keluhan=keluhan::findOrFail($id);

    dd($keluhan);
}
1 like
vipin93's avatar
vipin93
Best Answer
Level 13
form class="form-horizontal" role="form" method="POST" action="{{ '/editkeluhan/{{$keluhan->id}' }}" enctype="multipart/form-data">
                        {{ csrf_field() }} {{ method_field('PATCH') }}


<div>
    <label for="produk" class="col-md-4 control-label" name="produk">Nama Produk</label>

    <div class="col-md-6">
        <input id="produk" type="text" class="form-control" name="produk" value="{{ old('produk', $keluhan->produk) }}" required="true">
    </div>
</div>

and import keluhan model on the top "use App\keluhan" and follow correct name follow(Keluhan) and instead of findorfail u can use route model binding like

public function editkeluhan(keluhan $keluhan){
        $halaman="tindaklayanan";
        return view('layanankonsumen.editkeluhan',compact('keluhan','halaman'));
    }

    public function updatekeluhanadmin(keluhan $keluhan, Request $r){
        $halaman = 'tindaklayanan';  //and i dont know why u using this     
        $keluhan->update($r->all());
        return redirect('/');
    }

after this change your route

Route::get('editkeluhan/{keluhan}','AdminController@editkeluhan');
Route::patch('editskeluhan/{keluhan}', 'AdminController@updatekeluhannadmin');
1 like
tisuchi's avatar

Can you provide any valid id from your database table?

For example-

$keluhan=keluhan::findOrFail(1);

In this case, in keluhan table, there is a column named id and it has value 1. So, $keluhan should return all the values from the table where id=1. It will works like this way-

SELECT * FROM keluhan WHERE id = 1
1 like
Christianus's avatar

mr @tisuchi yes sir im sure about it and i've check it, so where's my fault ?

A sec i'll give my full code of view, route, and controller

Christianus's avatar

So it begin with DaftarKeluhan (it show all my data table)

@extends('template')

@section ('main')
<div class="row">
    <div class="col s12 m12">
        <h4>Keluhan Konsumen</h4><hr>
        @if (!empty($rows))
          <table class="striped bordered">
            <thead>
                <tr>
                  <th hidden="true">id</th>
                  <th>Tanggal</th>
                  <th>Produk</th>                                 
                  <th>Pelanggan</th>
                  <th>Area</th>
                  <th>Masalah</th>                                 
                  <th>Status</th>         
                @if (Auth::check() && (Auth::user()->level == 'MR' || Auth::user()->level == 'PDD'))
                  <th colspan="3" style="text-align:center;">Action</th>
                @else
                  <th style="text-align:center;">Action</th>
                @endif
              </tr>
            </thead>
            <tbody>                        
              <?php foreach($rows as $value): ?>
                <tr>
                  <td hidden="true">{{$value->id}}</td>
                  <td>{{$value->tanggal}}</td>
                  <td>{{$value->produk}}</td>                                 
                  <td>{{$value->username}}</td>
                  <td>{{$value->area}}</td>
                  <td>{{$value->masalah}}</td>                                 
                  <td>{{$value->status}}</td>
                  <td>                            
                  <a href="{{URL::to('detailkeluhan/'. $value->id)}}" class="btn" Detail</a>Detail</td>
                  @if (Auth::check() && (Auth::user()->level == 'MR' || Auth::user()->level == 'PDD'))
                    <td><a href="{{URL::to('editkeluhan/'. $value->id)}}" class="btn">Edit</a>
                    <td><a href="{{URL::to('hapuskeluhan/'. $value->id)}}" class="btn">Hapus</a>
                  @endif
                  </td>                                
                  </td>
                </tr>
              <?php endforeach?>
            </tbody>
        </table>
      @else
        <h5>Tidak ada data keluhan</h5>
      @endif
    </div>
</div>        
@endsection

My DELETE function is work fine now i work on EDIT Route Here

Route::get('editkeluhan/{id}','AdminController@editkeluhan');
Route::patch('editkeluhanadmin/{id}', 'AdminController@updatekeluhanadmin');

AdminController is Here

public function editkeluhan($id){
        $halaman="tindaklayanan";
        $keluhan=keluhan::findOrFail($id);
        return view('layanankonsumen.editkeluhan',compact('keluhan','halaman'));
    }

    public function updatekeluhanadmin($id,Request $request){
        $halaman = 'tindaklayanan';
        $keluhan=keluhan::findOrFail($id);
        $keluhan->produk=$request->produk;
        $sasaran->save();
        return redirect('/');
    }

And my problem View is here

@extends('template')

@section('main')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading"><h4>Keluhan Pelanggan<h4></div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="PATCH" action="{{ url('/editkeluhan/{id}') }}" enctype="multipart/form-data">
                        {{ csrf_field() }}

                        <div>
                            <input type="hidden" name="_method" value="PATCH">
                            <input name="id" value="{{old('id', $keluhan->id)}}">
                        <div> 

                        <div class="form-group">
                            <label for="unit" class="col-md-4 control-label">Pilih Unit</label>
                            <div class="col-md-6">
                                <select name="unit">
                                    <option value="Quality Control">Quality Control</option>
                                    <option value="Customer Service">Customer Service</option>
                                    <option value="PPIC">PPIC</option>
                                    <option value="Produksi 1">Produksi 1</option>
                                    <option value="Produksi 2">Produksi 2</option>
                                    <option value="Finishing">Finishing</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="jenis_keluhan" class="col-md-4 control-label">Jenis Keluhan</label>
                            <div class="col-md-6">
                                <select name="jenis_keluhan">
                                    <option value="Reject">Komplain</option>
                                    <option value="Complain">Reject</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="status" class="col-md-4 control-label">Status Keluhan</label>
                            <div class="col-md-6">
                                <select name="status">
                                    <option value="Pending">Pending</option>
                                    <option value="Proses">Proses</option>
                                    <option value="Selesai">Selesai</option>
                                </select>
                            </div>
                        </div>

                        <div>
                            <label for="produk" class="col-md-4 control-label" name="produk">Nama Produk</label>

                            <div class="col-md-6">
                                <input id="produk" type="text" class="form-control" name="produk" value="{{old('produk', $keluhan->produk)}}" required="true">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="area" class="col-md-4 control-label">Pilih Area</label>
                            <div class="col-md-6">
                                <select name="area">
                                    <option value="Jawa Barat">Jawa Barat</option>
                                    <option value="Jawa Tengah">Jawa Tegah</option>
                                    <option value="Jawa Timur">Jawa Timur</option>
                                    <option value="Luar Area Jawa">Luar Area Jawa</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="kategori" class="col-md-4 control-label">Kategori</label>
                            <div class="col-md-6">
                                <select name="kategori">
                                    <option value="Keluhan Pelanggan">Keluhan Pelanggan</option>
                                    <option value="Survey Pelanggan">Survey Pelanggan</option>
                                    <option value="Inspeksi">Inspeksi</option>
                                    <option value="Audit">Audit</option>
                                    <option value="Gagal Proses">Gagal Proses</option>
                                    <option value="Analisa Data">Analisa Data</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="masalah" class="col-md-4 control-label" required="true">Keluhan Pelanggan</label>
                            <textarea style="resize: none; height: 100px" name="masalah">{{old('masalah', $keluhan->masalah)}}</textarea>
                        </div>

                        <div class="form-group">
                            <label for="keterangan" class="col-md-4 control-label">Keterangan Tambahan</label>
                            <textarea style="resize: none; height: 100px" name="keterangan">{{old('keterangan', $keluhan->keterangan)}}</textarea><p>
                        </div>


                        <div align="center">
                            <div>
                                <h7><b>Tambahkan Gambar</b></h7>
                                <h7>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</h7>
                                <h7><b>Disetujui Oleh (PuraBox)</b></h7>
                            </div>
                            <div>
                                <input class="field" name="gambarkeluhan" type="file" required="true">
                                <input class="field" name="ttdcus2" type="file" required="true">
                                <br>
                                <br>
                                <br>
                                <br><br>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4" align="center">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fa fa-btn fa-sign-in"></i> Lanjut
                                </button>                            
                            </div> 
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<br>
<br>
<br>
<br><br>
@endsection
Christianus's avatar

@vipin93 okay i'll check and try it bro, btw $halaman active is used for shown active page only, and idk about request $r, where i get 'r' ? from whick form ?

vipin93's avatar

did u try what i commented if yes then whats the errors

Snapey's avatar

You cannot put method="PATCH" in your form as I explained you must use method="POST" and then include the {{method_field('PATCH')}} as a separate line in your form.

Next, your action is wrong action="{{ url('/editkeluhan/{id}') }}

{id} will be passed exactly as that so you will be searching in your model for {id}

Change it as per instruction from @vipin93 action="/editkeluhan/{{$keluhan->id }}"

Christianus's avatar

@vipin93 thank you vipin to always reply, btw i've follow ur instruction except this line$keluhan->update($r->all()); i still not understand what is 'r' and where it come from brother

@snapey oh sorry my bad, i'll change the method sir, wait..

Christianus's avatar

@vipin93 this is my error message after MethodNotAllowedHttpException

Controller

public function editkeluhan($id){
        $halaman="tindaklayanan";
        $keluhan=keluhan::findOrFail($id);
        return view('layanankonsumen.editkeluhan',compact('keluhan','halaman'));
    }

    public function updatekeluhanadmin(Keluhan $keluhan, Request $r){
        $halaman = 'tindaklayanan';
        $keluhan->update($r ->all());
        return redirect('/');
    }

editkeluhan.blade.php

<form class="form-horizontal" role="form" method="POST" action="{{ url('/editkeluhanadmin/{$keluhan->id}') }}" enctype="multipart/form-data">
                        {{ csrf_field() }} {{ method_field('PATCH')}}

Route

//edit keluhan
Route::get('editkeluhan/{id}','AdminController@editkeluhan');
Route::post('editkeluhanadmin/{id}', 'AdminController@updatekeluhanadmin');

Error

MethodNotAllowedHttpException in RouteCollection.php line 218:
in RouteCollection.php line 218
at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 205
at RouteCollection->getRouteForMethods(object(Request), array('POST')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 821
at Router->findRoute(object(Request)) in Router.php line 691
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54
Christianus's avatar

@vipi93 @Snapey I've try to change post on route with resource and found the new error (idk this is better or worse)

Route::resource('editkeluhanadmin/{id}', 'AdminController@updatekeluhanadmin');

error

Route pattern "/editkeluhanadmin/{id}/{{id}}" cannot reference variable name "id" more than once.
in RouteCompiler.php line 102
at RouteCompiler::compilePattern(object(Route), '/editkeluhanadmin/{id}/{{id}}', false) in RouteCompiler.php line 57
at RouteCompiler::compile(object(Route)) in Route.php line 565
at Route->compile() in Route.php line 214
at Route->compileRoute() in Route.php line 186
at Route->matches(object(Request), true) in RouteCollection.php line 232
at RouteCollection->Illuminate\Routing\{closure}('editkeluhanadmin/{id}/{{id}}', object(Route))
at call_user_func(object(Closure), 'editkeluhanadmin/{id}/{{id}}', object(Route)) in Arr.php line 163
vipin93's avatar

if try to use resource then no need of use of again /{id}

Route::resource('editkeluhanadmin', 'AdminController@updatekeluhanadmin');

use withoute "/{id}" and run php artisan route:list u will find your what route ushould use

Christianus's avatar

@vipin93 Okay now the error is gone and looks better, but it doesn't update the value bro, what should i do ? need some touch again

    public function updatekeluhanadmin(Keluhan $keluhan, Request $r){
        $halaman = 'tindaklayanan';
        $keluhan->update($r ->all());
        return redirect('/');
Snapey's avatar

Your form is still wrong. Please write action exactly as I stated

In your controller, you appear to have a space in here $keluhan->update($r ->all());

Next

Please or to participate in this conversation.