Home > front end >  Can't delete selected item, only delete last item added in laravel
Can't delete selected item, only delete last item added in laravel

Time:03-28

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 route in web.php

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

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. when I return the results of the $master_cuti data I get the last data I entered, not the data I selected

 public function delete_master_cuti(Request $request) {

        $master_cuti = CompanyMasterCuti::where($request->company_id)->where($request->year);
        $master_cuti->delete();

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

    }

in index.blade.php I have defined the data to be deleted based on the selected company_id and year

<a href="javascript:void(0)" onclick="$('#company_id','#year').val($(this).data(['company_id','year'])); $('#deleteModalBu').modal('show');">                                                            
       <i ></i>
</a>

form delete modal

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

                            <div >
                                <div >
                                    <button type="button"  data-dismiss="modal"
                                        style="width: 100%;">
                                        <i ></i>
                                        <span >CANCEL</span>
                                    </button>
                                </div>
                                <div >
                                    <button type="submit"  style="width: 100%">
                                        <i ></i>
                                        <span >DELETE</span>
                                    </button>
                                </div>
                            </div>

                        </div>
       </form>
                

I want to delete data based on the company_id and year I chose, but why is the deleted data the last time I entered? how to solve my problem? anyone, help me.

CodePudding user response:

you must specify the column name (company_id and year) in the query. for example :

public function delete_master_cuti(Request $request) {

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

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

}

CodePudding user response:

Have you check the response from "public function delete_master_cuti(Request $request)" by return $request->all(), whether company_id & year in method?

  • Related