Linked Questions

Popular Questions

Deleting records from database in Laravel

Asked by At

When I try to delete a record, it always deletes the last record in the database table instead of the one I wanted to delete.

Im fairly new to Laravel and I've exhausted all my options.



           Route::delete('/deleterecord/{record}'[RecordsController::class, 'destroy'])->name('destroyrecord');



public function destroy(Records $record) {

        return redirect()->route('users.viewrecord')->with('success', 'Customer deleted     successfully');


<!-- DataTales Example -->
       <div class="card shadow mb-4">
           <div class="card-header py-3">
               <h6 class="m-0 font-weight-bold text-primary">Records</h6>
           <div class="card-body">
               <div class="table-responsive">
                   <table class="table-sm table-bordered" id="dataTable" width="100%" cellspacing="0">
                               <th width="10%">ID</th>
                               <th width="30%">Name</th>
                               <th width="15%">Student No</th>
                               <th width="10%">Batch No</th>
                               <th width="15%">Mobile</th>
                               <th width="20%">Email</th>
                               <th width="10%">Action</th>

                           @foreach ($records as $record)
                               <td>{{ $record->id }}</td>
                               <td>{{ $record->name }}</td>
                               <td>{{ $record->student_number }}</td>
                               <td>{{ $record->batch_number }}</td>
                               <td>{{ $record->mobile_number }}</td>
                               <td>{{ $record->email }}</td>

                               <td style="display: flex">
                                   <a href="{{ route('users.editrecord', ['record' => $record->id]) }}" class="btn-sm btn-primary m-2">
                                       <i class="fa fa-pen"></i>
                                   <a class="btn-sm btn-danger m-2" href="#" data-toggle="modal" data-target="#deleteRecordModal">
                                       <i class="fas fa-trash"></i>



<div class="modal fade" id="deleteRecordModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalExample"
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="deleteModalExample">Delete Customer</h5>
                <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
            <div class="modal-body">Are you sure you want to delete?</div>
            <div class="modal-footer">
                <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
                <a class="btn btn-danger" href="{{ route('logout') }}"
                    onclick="event.preventDefault(); document.getElementById('record-delete-form').submit();">
                <form id="record-delete-form" method="POST" action="{{ route('users.destroyrecord', $record->id) }}">

I tried running dd($record); in the controller, it shows no matter which record I delete, the ID that always gets deleted is the last one in the database table.

Related Questions