Soni Silalahi
Soni Silalahi

Reputation: 249

NotFoundHttpException in RouteCollection.php (line 179) when delete data from table

so, the view page already display all data. but there is an error "not found exception" when the delete button will be pressed. URL that showed in my browser when i press delete button is "http://localhost:8000/admin/hapusdataruang/69"

This is the view page

          <table id="datatable-buttons" class="table table-striped table-bordered">
                  <thead>
                        <tr>
                            <th>ID Ruang</th>
                            <th>Nama Ruangan</th>
                            <th>Keterangan</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                  <tbody>
                        @foreach($showruang as $rooms)
                      <tr>
                        <td>{{$rooms->id_ruang}}</td>
                        <td>{{$rooms->nm_ruang}}</td>
                        <td>{{$rooms->keterangan}}</td>
                        <td>
                            <form action="{{ url('/admin/hapusdataruang', $rooms->id_ruang) }}" method="post">
                            {{ csrf_field() }}
                            {{ method_field('DELETE') }}
                            <a href="{{ url('/admin/editdataruang',$rooms->id_ruang) }}" class=" btn btn-sm btn-primary">Edit</a>
                            <button class="btn btn-sm btn-danger" type="submit" onclick="return confirm('Yakin ingin menghapus data?')">Delete</button>
                            </form>
                        </td>
                    </tr>
                   @endforeach
                    </tbody>

This is the AdminController

public function destroyruang($id_ruang)
    {
        $ruang = Ruang::where('id_ruang',$id_ruang)->first();
        $ruang->delete();
        return redirect(url('/admin/dataruang'));
    }

This is the admin route

 Route::post('/hapusdataruang', 'AdminController@destroyruang', function () {
    $users[] = Auth::user();
    $users[] = Auth::guard()->user();
    $users[] = Auth::guard('admin')->user();
    //dd($users);
})->name('destroydataruang');

This is Ruang Model

use Illuminate\Database\Eloquent\Model;
namespace App;

    class Ruang extends Model
    {
        protected $table = 'tr_ruang';

        protected $primaryKey = 'id_ruang';

        protected $dates = ['deleted_at'];
        protected $fillable = ['keterangan','nm_ruang'];
    }

Upvotes: 0

Views: 241

Answers (3)

Ankur Tailang
Ankur Tailang

Reputation: 21

If You want to use the POST Request for deleting an entry from your database then you don't have to specify {{ method_field('DELETE') }} in your form, if you remove this from your form declaration then your existing route will work, but if you want to use the DELETE Request then you have to specify the Route as Route::delete('/yourpath','ControllerName@methodname');

Upvotes: 0

Benjamin Dowson
Benjamin Dowson

Reputation: 579

The problem is in your routes file. You are submitting the form via a POST request to this url /admin/hapusdataruang, but you have {{ method_field('DELETE') }} in your form, so your route needs to be able to accept DELETE requests.

Your code:

 Route::post('/hapusdataruang', 'AdminController@destroyruang', function () {

is for POST requests, not DELETE requests, so change it to:

 Route::delete('/hapusdataruang', 'AdminController@destroyruang', function () {

Upvotes: 1

Reza1607
Reza1607

Reputation: 58

change this code

 Route::post('/hapusdataruang', 'AdminController@destroyruang', function () {

to

 Route::delete('/hapusdataruang', 'AdminController@destroyruang', function () {

Upvotes: 0

Related Questions