Home > other >  laravel can't delete a row with one to many relation
laravel can't delete a row with one to many relation

Time:10-16

I have one to many relation in my project ( the user has many details ), and when i delete any user row it says

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`sehhatys_saudi`.`details`, CONSTRAINT `details_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) (SQL: delete from `users` where `id` = 9)

the details table migration

Schema::create('details', function (Blueprint $table) {
        $table->id();
        $table->bigInteger('user_id')->unsigned();
        $table->string('check_date', 100);
        $table->string('result_date', 100);
        $table->string('result', 100);
        $table->string('check_center', 100);
        $table->timestamps();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });

the User model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasFactory;

    protected $fillable = ['name', 'name_en', 'national_id', 
'birth_date', 'user_password', 'nationality', 'nationality_en', 
'vaccine_type_one', 'vaccine_type_one_en', 'date_one', 
'batch_number_one', 'vaccine_type_two', 'vaccine_type_two_en', 
'date_two', 'batch_number_two', 'report_id', 'qr_url', 'access_token', 
'phone', 'email', 'social', 'social_en', 'weight', 'height', 'mass', 
'blood_pressure', 'blood_sugar', 'waist', 'hypertension', 
'hypertension_en', 'diabetes', 'diabetes_en', 'city', 'city_en', 
'health_center', 'health_center_en'];

    public function details(){
        return $this->hasMany('App\Detail');
    }
}

the Detail model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Detail extends Model
{
    use HasFactory;

    protected $fillable = ['user_id', 'check_date', 'result_date', 
'result', 'check_center'];

    public function user(){
        return $this->belongsTo('App\User');
    }
}

the delete function for the user row

public function delete($id)
{
    User::findOrFail($id)->delete();

    return redirect(route('users.all'));
}

even when edited the delete function and make it like this

public function delete($id)
{
    $user = User::findOrFail($id);

    $user->details()->delete();
    $user->delete();

    return redirect(route('users.all'));
}

it returns this error

Class "App\Detail" not found

CodePudding user response:

The name space for your Detail Model is namespace App\Models; While you are using App\Detail in User.php.

The relation in User model should be:

public function details(){
    return $this->hasMany('App\Models\Detail');
}

If you are using laravel-8 it should be like:

public function details(){
    return $this->hasMany(App\Models\Detail::class);
}
  • Related