random student
random student

Reputation: 775

Unable to update data with Laravel does not show any error

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 it makes me more confused of what's wrong because i really don't know why.

Route for edit and update

Route::get('contact/{contact}/edit', 'ContactController@edit');
Route::post('contact/update','ContactController@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="/contact/update" method="POST">
        @csrf
        <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>

Upvotes: 0

Views: 5337

Answers (2)

user8034901
user8034901

Reputation:

Your update route is using the wrong "verb" and url. If you take a look at Laravel's Resource Controllers you can see the different actions and route names available for editing, updating, deleting etc. when creating a "CRUD" controller.

You can see the route for an "update" action and its "verb".

Change your routes to

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

Or if you want to add a complete CRUD controller use the short form:

Route::resource('contact', 'ContactController');

This will create all required routes in one convenient line of code. Use php artisan route:list to check all routes.

HTTP forms only support GET and POST methods, Laravel uses @method() in blade to add the other verbs (put,patch,delete):

Edit:

your form uses disabled attributes on some <input>s. Those values will not be sent along with your request. Here's an updated edit.blade.php:

  • disabled attributes have been swapped with readonly.
  • The action uses Laravel's RouteModelBinding
  • Removed the @foreach since you only have one item to be edited

edit.blade.php:

<div id="contact">
    <h2>Edit Contact</h2>
    <form action="{{ route('contact.update', ['contact' => $contact]) }}" 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="{{ $contact->kode_kontak}}" readonly>
        </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="{{ $contact->kode_pegawai}}" readonly>
        </div>
        <div class="form-group">
            <label for="email" class="control-label">Email</label>
            <input type="text" name="email" id="email" class="form-control" value="{{ $contact->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="{{ $contact->telepon}}">
        </div>
        <div class="form-group">
            <input class="btn btn-primary form-control" type="submit" value="Simpan">
        </div>
    </form>
</div>

Since it's using RouteModelBinding you can change your update() method to:

public function update(Request $request, Contact $contact)
{
    $contact->update([
        'email' => $request->email,
        'telepon' => $request->telepon,
    ]);
    return redirect('contact');
}

Laravel will know what Contact $contact is

Upvotes: 1

IBRAHIM EZZAT
IBRAHIM EZZAT

Reputation: 983

you loop your contact to produce a form for each one of them with the same input name, id,... which is the wrong approach. my suggestion to you : Route for edit and update

Route::get('contact/{contact}/edit', 'ContactController@edit');
Route::post('contact/{contact}/edit', 'ContactController@update');

Controller with edit and update function

public function edit(Contact $contact){

  return view('contact.edit',compact('contact'));
}

public function update(Request $request,Contact $contact){

        $contact->update([
            'email' => $request->email,
            'telepon' => $request->telepon,
        ]);

        return redirect('contact');
    }

and of course, you will update edit.blade.php

<div id="contact">
    <h2>Edit Contact</h2>
    @foreach($contact as $p)
        <form action="/contact/{{ $p->id }}/update" method="POST">
        @csrf

        <div class="form-group">
            <label for="kode_contact" class="control-label">Kode Kontak</label>
            <input type="text" name="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" 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" class="form-control" value="{{ $p->email}}">
        </div>
        <div class="form-group">
            <label for="telepon" class="control-label">Telepon</label>
            <input type="text" name="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>

Upvotes: 1

Related Questions