Emad Al-Kabi
Emad Al-Kabi

Reputation: 43

why delete and destroy methods not deleting model - laravel

I'm trying to delete record from database I had write this code :

Route::get("/delete-student/{id}" , function ($id)
{
    $student = \App\Student\Student::find($id);
    $success = $student->delete();
    return ["success" => $success];
});

the result is ture but the record is still in database , I also tried this

$student = \App\Student\Student::destory($id);

but the record still not deleted

here is my student model

class Student extends Model
{
    protected $table = "student";
}

Upvotes: 1

Views: 1332

Answers (3)

Ali Faris
Ali Faris

Reputation: 18592

i had run to the same problem , the issue was because of the name of columns in my schema

laravel assume that each table has a primary key column named "id" , so if your primary key is not identical to "id" with lowercase you should override that by adding $primaryKey property to your model like this

protected $primaryKey = “YOUR_EXACT_KEY” ; //in my case was “Id”

Upvotes: 4

Sagar Gautam
Sagar Gautam

Reputation: 9369

Instead of find and delete you can directly delete the rows as follows

Route::get("/delete-student/{id}" , function ($id)
{
    $success = \App\Student\Student::where('id',$id)->delete();

    return ["success" => $success];
});

This will work in your case.

Upvotes: 0

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

I think you can try this :

Route::get("/delete-student/{id}" , function ($id)
{
    $student = \App\Student\Student::find($id);
    $success = $student->forceDelete();
    return ["success" => $success];
});

Hope this work for you !!!

Upvotes: 0

Related Questions