delete data based on company_id and year from database in laravel

Viewed 62

I want to delete data based on company_id and year from my database. enter image description here

sample data content

enter image description here

my model in App\Models, I've added the company_id and year to be for the primary key

class CompanyMasterCuti extends Model
{
    use HasFactory;

    protected $table = 'company_master_cuti';
    protected $fillable = [
        'company_id', 'year', 'cuti', 'created', 'created_by', 'modified', 'modified_by', 
    ];
    protected $guarded = [];
    protected $keyType = 'string';
    public $timestamps = false;
    protected $primaryKey = ['company_id', 'year'];
    public $incrementing = false;

    public function company() {
        return $this->belongsTo('App\Models\Company', 'company_id', 'id');
    }
}

my code in controller


    public function delete_master_cuti($company_id, $year) {

        $master_cuti = CompanyMasterCuti::where('company_id', $company_id)->where('year', $year)->first();

        $master_cuti->delete();

        toast('Success', 'success');
        return redirect()->route('master-cuti.index');

    }

index.blade.php, in this code I've added the requested company_id and year to delete the data, but this method still doesn't work

<a href="javascript:void(0)" onclick="$('#master_cuti_ids').val($(this).data('company_id','year')); $('#deleteModalBu').modal('show');">
         <li class="badge-circle badge-circle-light-secondary bx bxs-trash font-medium-1 text-danger my-1"></i>
 </a>                                                          

enter image description here

form action to delete function delete data in index.blade.php

   <form id="form-edit" action="{{ route('delete_master_cuti', [$m_cuti->company_id, $m_cuti->year] ) }}" method="POST">
                        @csrf
                        <div class="modal-body">
                            <input type="hidden" id="company_id" name="company_id">
                            <input type="hidden" id="year" name="year">

                            <div class="row no-gutters">
                                <div class="col-md-6">
                                    <button type="button" class="btn btn-light-secondary" data-dismiss="modal"
                                        style="width: 100%;">
                                        <i class="bx bx-x d-block d-sm-none"></i>
                                        <span class="d-none d-sm-block">Cancel</span>
                                    </button>
                                </div>
                                <div class="col-md-6">
                                    <button type="submit" class="btn btn-danger ml-1" style="width: 100%">
                                        <i class="bx bx-check d-block d-sm-none"></i>
                                        <span class="d-none d-sm-block">Delete</span>
                                    </button>
                                </div>
                            </div>

                        </div>
                    </form>

my route

    Route::post('delete_master_cuti/{company_id}/{year}', [CompanyMasterCutiController::class, 'delete_master_cuti'])->name('delete_master_cuti');

I get error: Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException The GET method is not supported for this route. Supported methods: POST. Please help me

0 Answers
Related