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?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2KzYDGd
via IFTTT

تعليقات

المشاركات الشائعة من هذه المدونة

I am unable to figure out how to create payment collection request, after a form has been submitted in laravel

laravel, mysql transaction not working after failed one time