mardi 21 avril 2020

How to delete a row without affecting reference table using Elequent model in Laravel 6?

I have two models called Parents and Childs.

Parent model have:-

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

Child model have:-

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

parent table has the following data

id             name            data
1              ABC             ABCDEF
2              EFG             HIJKLM

child table has the following data

id             name            data                parent_id
1              123             123456                 1
2              456             789101112              1

I just want to delete the first row of child table using Eloquent models. It should not affect parent table.

I tried:-

$child=Childs::find($id);
$child->delete();

and

$child=Childs::find($id);
$child->parents()->delete();
$child->delete();

In either way, the first row of parent table will be deleted. I want to keep all data in the parent table and delete the first row of child data. How can I achieve that?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire