2

I'm trying to update my data with Laravel. I'm able to create, read, delete the data but somehow i cannot update my data. I already checked my controller,model,route and view but i don't think there's any typo or anything. It only redirects to it's index page without being updated although i have entered new input. There's no error message at all so i checked where is the problem. So i checked my update function in my controller and tried to show the request by echo "$request->kode_kontak"; and echo $request->kode_kontak; but it shows nothing which i assume that it's null/empty but when i echo "yes" it showed on the screen "yes" i tested this because i want to know if the function itself is working so the problem here is that the request contains null, no wonder i cannot update it. Why is the request isn't passed? why is it like this? and how to fix it?

Route for edit and update

Route::get('contact/{contact}/edit', 'ContactController@edit')->name('contact.edit');
Route::patch('contact/{contact}','ContactController@update')->name('contact.update');

Controller with edit and update function

use Illuminate\Http\Request;
use App\Contact;
use DB;

public function edit($kode_kontak){
        $contact = DB::table('contact')->where('kode_kontak',$kode_kontak)->get();
        return view('contact.edit',['contact' => $contact]);
    }

public function update(Request $request){
        DB::table('contact')->where('kode_kontak',$request->kode_kontak)->update([
            'email' => $request->email,
            'telepon' => $request->telepon,
        ]);
        return redirect('contact');
    }

Model

class Contact extends Model
{
    public $timestamps = false;
    
    protected $table = 'contact';

    protected $fillable = [
        'kode_kontak',
        'kode_pegawai',
        'email',
        'telepon'
        
    ];

    protected $primaryKey = 'kode_kontak';
}

View of edit.blade.php

<div id="contact">
    <h2>Edit Contact</h2>
    @foreach($contact as $p)
        <form action="{{ route('contact.update', ['kode_pegawai' => $p->kode_pegawai]) }}" method="POST">
    @csrf
    @method('patch')
        <div class="form-group">
            <label for="kode_contact" class="control-label">Kode Kontak</label>
            <input type="text" name="kode_kontak" id="kode_kontak" class="form-control" value="{{ $p->kode_kontak}}" disabled>
        </div>
        <div class="form-group">
            <label for="kode_pegawai" class="control-label">Kode Pegawai</label>
            <input type="text" name="kode_pegawai" id="kode_pegawai" class="form-control" value="{{ $p->kode_pegawai}}" disabled>
        </div>
        <div class="form-group">
            <label for="email" class="control-label">Email</label>
            <input type="text" name="email" id="email" class="form-control" value="{{ $p->email}}">
        </div>
        <div class="form-group">
            <label for="telepon" class="control-label">Telepon</label>
            <input type="text" name="telepon" id="telepon" class="form-control" value="{{ $p->telepon}}">
        </div>
        <div class="form-group">
            <input class="btn btn-primary form-control" type="submit" value="Simpan">
        </div>
    </form>
    @endforeach
</div>
random student
  • 683
  • 1
  • 15
  • 33

2 Answers2

2

Your issue is that you have disabled those inputs. Disabled inputs will not be submitted.

If you want to display the disabled inputs, but still PATCH the values, you will need to add hidden inputs with those values like:

        <div class="form-group">
            <label for="kode_contact" class="control-label">Kode Kontak</label>
            <input type="text" id="kode_kontak" class="form-control" value="{{ $p->kode_kontak}}" disabled>
            <input type="hidden" name="kode_kontak" value="{{ $p->kode_kontak}}">
        </div>
        <div class="form-group">
            <label for="kode_pegawai" class="control-label">Kode Pegawai</label>
            <input type="text" id="kode_pegawai" class="form-control" value="{{ $p->kode_pegawai}}" disabled>
            <input type="hidden" name="kode_pegawai" value="{{ $p->kode_pegawai}}">
        </div>

Hope that helps!

Kurt Friars
  • 3,625
  • 2
  • 16
  • 29
  • @potatostudent For debugging purposes try removing the disabled property on the inputs and submit, then inspect the request. If the values are still missing I will delete my answer. – Kurt Friars Jul 09 '20 at 10:39
  • I am going to leave this here, since I am quite confident this is related/the root cause of your issue. You have 2 disabled inputs not submitting their values (which they shouldn't), and two non-disabled inputs that are submitting their values. – Kurt Friars Jul 09 '20 at 10:52
  • @Kurt, `$kode_kontak` is not available on dd – STA Jul 09 '20 at 10:54
  • @STA what about in network inspect request body? I will start trying to debug locally too – Kurt Friars Jul 09 '20 at 10:55
  • @KurtFriars but i still need to show the value of `kode_kontak` because i need to know which `kode` or `id` that i'm editing – random student Jul 09 '20 at 11:01
  • @potatostudent So the it works when you remove disabled from the text inputs, but not when you use the hidden input approach? – Kurt Friars Jul 09 '20 at 11:02
  • @KurtFriars what you said works for me, exactly what you said – random student Jul 09 '20 at 11:04
  • @potatostudent Ok, I just want to confirm, since we may be having English issues here, this answer completely solves your issue? – Kurt Friars Jul 09 '20 at 11:06
1

$request->kode_kontak is $contact here, $request->kode_kontak is not available in $request, change $contact instead :

public function update(Request $request, $contact){
   DB::table('contact')->where('kode_kontak',$contact)->update([
      'email' => $request->email,
      'telepon' => $request->telepon,
   ]);
  return redirect('contact');
}
STA
  • 30,729
  • 8
  • 45
  • 59