How to soft delete related records when soft deleting a parent record in Laravel?
up vote
12
down vote
favorite
I have this invoices table that which has the following structure
id | name | amount | deleted_at
2 iMac 1500 | NULL
and a payments table with the following structure
id | invoice_id | amount | deleted_at
2 2 1000 | NULL
Invoice Model
class Invoice extends Model {
use SoftDeletes;
}
here's the code to delete the invoice
public function cance(Request $request,$id)
{
$record = Invoice::findOrFail($id);
$record->delete();
return response()->json([
'success' => 'OK',
]);
}
Payments model
class Payment extends Model {
use SoftDeletes;
}
The softDelete on Invoice table works perfectly but its related records (payments) still exists.How do I delete them using softDelete?
php laravel laravel-5 eloquent soft-delete
add a comment |
up vote
12
down vote
favorite
I have this invoices table that which has the following structure
id | name | amount | deleted_at
2 iMac 1500 | NULL
and a payments table with the following structure
id | invoice_id | amount | deleted_at
2 2 1000 | NULL
Invoice Model
class Invoice extends Model {
use SoftDeletes;
}
here's the code to delete the invoice
public function cance(Request $request,$id)
{
$record = Invoice::findOrFail($id);
$record->delete();
return response()->json([
'success' => 'OK',
]);
}
Payments model
class Payment extends Model {
use SoftDeletes;
}
The softDelete on Invoice table works perfectly but its related records (payments) still exists.How do I delete them using softDelete?
php laravel laravel-5 eloquent soft-delete
add a comment |
up vote
12
down vote
favorite
up vote
12
down vote
favorite
I have this invoices table that which has the following structure
id | name | amount | deleted_at
2 iMac 1500 | NULL
and a payments table with the following structure
id | invoice_id | amount | deleted_at
2 2 1000 | NULL
Invoice Model
class Invoice extends Model {
use SoftDeletes;
}
here's the code to delete the invoice
public function cance(Request $request,$id)
{
$record = Invoice::findOrFail($id);
$record->delete();
return response()->json([
'success' => 'OK',
]);
}
Payments model
class Payment extends Model {
use SoftDeletes;
}
The softDelete on Invoice table works perfectly but its related records (payments) still exists.How do I delete them using softDelete?
php laravel laravel-5 eloquent soft-delete
I have this invoices table that which has the following structure
id | name | amount | deleted_at
2 iMac 1500 | NULL
and a payments table with the following structure
id | invoice_id | amount | deleted_at
2 2 1000 | NULL
Invoice Model
class Invoice extends Model {
use SoftDeletes;
}
here's the code to delete the invoice
public function cance(Request $request,$id)
{
$record = Invoice::findOrFail($id);
$record->delete();
return response()->json([
'success' => 'OK',
]);
}
Payments model
class Payment extends Model {
use SoftDeletes;
}
The softDelete on Invoice table works perfectly but its related records (payments) still exists.How do I delete them using softDelete?
php laravel laravel-5 eloquent soft-delete
php laravel laravel-5 eloquent soft-delete
asked Aug 23 '15 at 5:32
user3407278
460928
460928
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
11
down vote
accepted
Eloquent doesn't provide automated deletion of related objects, therefore you'll need to write some code yourself. Luckily, it's pretty simple.
Eloquent models fire different events in different stages of model's life-cycle like creating, created, deleting, deleted etc. - you can read more about it here: http://laravel.com/docs/5.1/eloquent#events. What you need is a listener that will run when deleted event is fired - this listener should then delete all related objects.
You can register model listeners in your model's boot() method. The listener should iterate through all payments for the invoice being deleted and should delete them one by one. Bulk delete won't work here as it would execute SQL query directly bypassing model events.
This will do the trick:
class MyModel extends Model {
protected static function boot() {
parent::boot();
static::deleted(function ($invoice) {
$invoice->payments()->delete();
});
}
}
Doesn't work! FatalErrorException in Invoice.php line 18: Cannot make static method IlluminateDatabaseEloquentModel::boot() non static in class AppModelsInvoice
– user3407278
Aug 23 '15 at 7:07
Fixed, the function was missing the static modifier
– jedrzej.kurylo
Aug 23 '15 at 7:10
That worked beautifully! Thanks a lot! Does this cause any performance issues when soft deleting say about 100 records?
– user3407278
Aug 23 '15 at 7:18
You have to fetch all of them and then save each of them, so it's additional 101 queries... In such case you could set the deleted_at manually for related models, this will be less clean but will run only one SQL query. I'll update the answer in a second
– jedrzej.kurylo
Aug 23 '15 at 7:20
thank you! @jedrzej.kurylo !!
– user3407278
Aug 23 '15 at 7:30
add a comment |
up vote
9
down vote
You can go one of 2 ways with this.
The simplest way would be to override Eloquents delete()
method and include the related models as well e.g.:
public function delete()
{
$this->payments()->delete();
return parent::delete();
}
The above method should work just find but it seems a little bit dirty and I'd say it's not the preferred method within the community.
The cleaner way (IMO) would be to tap into Eloquents events e.g.:
public static function boot()
{
parent::boot();
static::deleting(function($invoice) {
$invoice->payments()->delete();
});
}
Either (but not both) of the above methods would go in your Invoice
model.
Also, I'm assuming that you have your relationships set up in your model, however, I'm not sure if you allow multiple payments for one invoice. Either way you might need to change the payments()
in the examples to whatever you've named the relationship in your invoice model.
Hope this helps!
Calling delete() on payments relation directly will bypass the model and won't trigger the SoftDelete on related models. They will be removed from the database. In order to make thek soft-deleted you need to call delete() on each of related models.
– jedrzej.kurylo
Aug 23 '15 at 6:38
You are also missing the return statement in your overridden delete method - it should do "return parent::delete();", otherwise you lose the value that would be returned from delete() if you hadn't overwritten it.
– jedrzej.kurylo
Aug 23 '15 at 6:54
@jedrzej.kurylo, I've just tested to make sure and YES you can use soft delete on a relationship!
– Ross Wilson
Aug 23 '15 at 7:30
True, just checked the code. Good to know for the future :)
– jedrzej.kurylo
Aug 23 '15 at 7:43
does it guarantee transaction @RossWilson
– Tomonso Ejang
Jul 20 at 8:07
|
show 3 more comments
up vote
4
down vote
I know you asked this question a long time ago but I found this package to be very simple and straightforward:
https://github.com/Askedio/laravel5-soft-cascade
Or you can use this package it's useful too:
https://github.com/michaeldyrynda/laravel-cascade-soft-deletes
Remember to install the right version depending on your laravel version.
You must install it via composer:
composer require askedio/laravel5-soft-cascade ^version
In second package:
composer require iatstuti/laravel-cascade-soft-deletes
Register the service provider in your config/app.php.
you can read the docs on the GitHub page.
If you delete a record this package recognizes all of its children and soft-delete them as well.
If you have another relationship in your child model use its trait in that model as well. its so much easier than doing it manually.
The second package has the benefit of deleting grandchildren of the model. in some cases, I say its a better approach.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
accepted
Eloquent doesn't provide automated deletion of related objects, therefore you'll need to write some code yourself. Luckily, it's pretty simple.
Eloquent models fire different events in different stages of model's life-cycle like creating, created, deleting, deleted etc. - you can read more about it here: http://laravel.com/docs/5.1/eloquent#events. What you need is a listener that will run when deleted event is fired - this listener should then delete all related objects.
You can register model listeners in your model's boot() method. The listener should iterate through all payments for the invoice being deleted and should delete them one by one. Bulk delete won't work here as it would execute SQL query directly bypassing model events.
This will do the trick:
class MyModel extends Model {
protected static function boot() {
parent::boot();
static::deleted(function ($invoice) {
$invoice->payments()->delete();
});
}
}
Doesn't work! FatalErrorException in Invoice.php line 18: Cannot make static method IlluminateDatabaseEloquentModel::boot() non static in class AppModelsInvoice
– user3407278
Aug 23 '15 at 7:07
Fixed, the function was missing the static modifier
– jedrzej.kurylo
Aug 23 '15 at 7:10
That worked beautifully! Thanks a lot! Does this cause any performance issues when soft deleting say about 100 records?
– user3407278
Aug 23 '15 at 7:18
You have to fetch all of them and then save each of them, so it's additional 101 queries... In such case you could set the deleted_at manually for related models, this will be less clean but will run only one SQL query. I'll update the answer in a second
– jedrzej.kurylo
Aug 23 '15 at 7:20
thank you! @jedrzej.kurylo !!
– user3407278
Aug 23 '15 at 7:30
add a comment |
up vote
11
down vote
accepted
Eloquent doesn't provide automated deletion of related objects, therefore you'll need to write some code yourself. Luckily, it's pretty simple.
Eloquent models fire different events in different stages of model's life-cycle like creating, created, deleting, deleted etc. - you can read more about it here: http://laravel.com/docs/5.1/eloquent#events. What you need is a listener that will run when deleted event is fired - this listener should then delete all related objects.
You can register model listeners in your model's boot() method. The listener should iterate through all payments for the invoice being deleted and should delete them one by one. Bulk delete won't work here as it would execute SQL query directly bypassing model events.
This will do the trick:
class MyModel extends Model {
protected static function boot() {
parent::boot();
static::deleted(function ($invoice) {
$invoice->payments()->delete();
});
}
}
Doesn't work! FatalErrorException in Invoice.php line 18: Cannot make static method IlluminateDatabaseEloquentModel::boot() non static in class AppModelsInvoice
– user3407278
Aug 23 '15 at 7:07
Fixed, the function was missing the static modifier
– jedrzej.kurylo
Aug 23 '15 at 7:10
That worked beautifully! Thanks a lot! Does this cause any performance issues when soft deleting say about 100 records?
– user3407278
Aug 23 '15 at 7:18
You have to fetch all of them and then save each of them, so it's additional 101 queries... In such case you could set the deleted_at manually for related models, this will be less clean but will run only one SQL query. I'll update the answer in a second
– jedrzej.kurylo
Aug 23 '15 at 7:20
thank you! @jedrzej.kurylo !!
– user3407278
Aug 23 '15 at 7:30
add a comment |
up vote
11
down vote
accepted
up vote
11
down vote
accepted
Eloquent doesn't provide automated deletion of related objects, therefore you'll need to write some code yourself. Luckily, it's pretty simple.
Eloquent models fire different events in different stages of model's life-cycle like creating, created, deleting, deleted etc. - you can read more about it here: http://laravel.com/docs/5.1/eloquent#events. What you need is a listener that will run when deleted event is fired - this listener should then delete all related objects.
You can register model listeners in your model's boot() method. The listener should iterate through all payments for the invoice being deleted and should delete them one by one. Bulk delete won't work here as it would execute SQL query directly bypassing model events.
This will do the trick:
class MyModel extends Model {
protected static function boot() {
parent::boot();
static::deleted(function ($invoice) {
$invoice->payments()->delete();
});
}
}
Eloquent doesn't provide automated deletion of related objects, therefore you'll need to write some code yourself. Luckily, it's pretty simple.
Eloquent models fire different events in different stages of model's life-cycle like creating, created, deleting, deleted etc. - you can read more about it here: http://laravel.com/docs/5.1/eloquent#events. What you need is a listener that will run when deleted event is fired - this listener should then delete all related objects.
You can register model listeners in your model's boot() method. The listener should iterate through all payments for the invoice being deleted and should delete them one by one. Bulk delete won't work here as it would execute SQL query directly bypassing model events.
This will do the trick:
class MyModel extends Model {
protected static function boot() {
parent::boot();
static::deleted(function ($invoice) {
$invoice->payments()->delete();
});
}
}
edited Nov 10 at 22:15
asachanfbd
1,0881923
1,0881923
answered Aug 23 '15 at 6:35
jedrzej.kurylo
25.1k54664
25.1k54664
Doesn't work! FatalErrorException in Invoice.php line 18: Cannot make static method IlluminateDatabaseEloquentModel::boot() non static in class AppModelsInvoice
– user3407278
Aug 23 '15 at 7:07
Fixed, the function was missing the static modifier
– jedrzej.kurylo
Aug 23 '15 at 7:10
That worked beautifully! Thanks a lot! Does this cause any performance issues when soft deleting say about 100 records?
– user3407278
Aug 23 '15 at 7:18
You have to fetch all of them and then save each of them, so it's additional 101 queries... In such case you could set the deleted_at manually for related models, this will be less clean but will run only one SQL query. I'll update the answer in a second
– jedrzej.kurylo
Aug 23 '15 at 7:20
thank you! @jedrzej.kurylo !!
– user3407278
Aug 23 '15 at 7:30
add a comment |
Doesn't work! FatalErrorException in Invoice.php line 18: Cannot make static method IlluminateDatabaseEloquentModel::boot() non static in class AppModelsInvoice
– user3407278
Aug 23 '15 at 7:07
Fixed, the function was missing the static modifier
– jedrzej.kurylo
Aug 23 '15 at 7:10
That worked beautifully! Thanks a lot! Does this cause any performance issues when soft deleting say about 100 records?
– user3407278
Aug 23 '15 at 7:18
You have to fetch all of them and then save each of them, so it's additional 101 queries... In such case you could set the deleted_at manually for related models, this will be less clean but will run only one SQL query. I'll update the answer in a second
– jedrzej.kurylo
Aug 23 '15 at 7:20
thank you! @jedrzej.kurylo !!
– user3407278
Aug 23 '15 at 7:30
Doesn't work! FatalErrorException in Invoice.php line 18: Cannot make static method IlluminateDatabaseEloquentModel::boot() non static in class AppModelsInvoice
– user3407278
Aug 23 '15 at 7:07
Doesn't work! FatalErrorException in Invoice.php line 18: Cannot make static method IlluminateDatabaseEloquentModel::boot() non static in class AppModelsInvoice
– user3407278
Aug 23 '15 at 7:07
Fixed, the function was missing the static modifier
– jedrzej.kurylo
Aug 23 '15 at 7:10
Fixed, the function was missing the static modifier
– jedrzej.kurylo
Aug 23 '15 at 7:10
That worked beautifully! Thanks a lot! Does this cause any performance issues when soft deleting say about 100 records?
– user3407278
Aug 23 '15 at 7:18
That worked beautifully! Thanks a lot! Does this cause any performance issues when soft deleting say about 100 records?
– user3407278
Aug 23 '15 at 7:18
You have to fetch all of them and then save each of them, so it's additional 101 queries... In such case you could set the deleted_at manually for related models, this will be less clean but will run only one SQL query. I'll update the answer in a second
– jedrzej.kurylo
Aug 23 '15 at 7:20
You have to fetch all of them and then save each of them, so it's additional 101 queries... In such case you could set the deleted_at manually for related models, this will be less clean but will run only one SQL query. I'll update the answer in a second
– jedrzej.kurylo
Aug 23 '15 at 7:20
thank you! @jedrzej.kurylo !!
– user3407278
Aug 23 '15 at 7:30
thank you! @jedrzej.kurylo !!
– user3407278
Aug 23 '15 at 7:30
add a comment |
up vote
9
down vote
You can go one of 2 ways with this.
The simplest way would be to override Eloquents delete()
method and include the related models as well e.g.:
public function delete()
{
$this->payments()->delete();
return parent::delete();
}
The above method should work just find but it seems a little bit dirty and I'd say it's not the preferred method within the community.
The cleaner way (IMO) would be to tap into Eloquents events e.g.:
public static function boot()
{
parent::boot();
static::deleting(function($invoice) {
$invoice->payments()->delete();
});
}
Either (but not both) of the above methods would go in your Invoice
model.
Also, I'm assuming that you have your relationships set up in your model, however, I'm not sure if you allow multiple payments for one invoice. Either way you might need to change the payments()
in the examples to whatever you've named the relationship in your invoice model.
Hope this helps!
Calling delete() on payments relation directly will bypass the model and won't trigger the SoftDelete on related models. They will be removed from the database. In order to make thek soft-deleted you need to call delete() on each of related models.
– jedrzej.kurylo
Aug 23 '15 at 6:38
You are also missing the return statement in your overridden delete method - it should do "return parent::delete();", otherwise you lose the value that would be returned from delete() if you hadn't overwritten it.
– jedrzej.kurylo
Aug 23 '15 at 6:54
@jedrzej.kurylo, I've just tested to make sure and YES you can use soft delete on a relationship!
– Ross Wilson
Aug 23 '15 at 7:30
True, just checked the code. Good to know for the future :)
– jedrzej.kurylo
Aug 23 '15 at 7:43
does it guarantee transaction @RossWilson
– Tomonso Ejang
Jul 20 at 8:07
|
show 3 more comments
up vote
9
down vote
You can go one of 2 ways with this.
The simplest way would be to override Eloquents delete()
method and include the related models as well e.g.:
public function delete()
{
$this->payments()->delete();
return parent::delete();
}
The above method should work just find but it seems a little bit dirty and I'd say it's not the preferred method within the community.
The cleaner way (IMO) would be to tap into Eloquents events e.g.:
public static function boot()
{
parent::boot();
static::deleting(function($invoice) {
$invoice->payments()->delete();
});
}
Either (but not both) of the above methods would go in your Invoice
model.
Also, I'm assuming that you have your relationships set up in your model, however, I'm not sure if you allow multiple payments for one invoice. Either way you might need to change the payments()
in the examples to whatever you've named the relationship in your invoice model.
Hope this helps!
Calling delete() on payments relation directly will bypass the model and won't trigger the SoftDelete on related models. They will be removed from the database. In order to make thek soft-deleted you need to call delete() on each of related models.
– jedrzej.kurylo
Aug 23 '15 at 6:38
You are also missing the return statement in your overridden delete method - it should do "return parent::delete();", otherwise you lose the value that would be returned from delete() if you hadn't overwritten it.
– jedrzej.kurylo
Aug 23 '15 at 6:54
@jedrzej.kurylo, I've just tested to make sure and YES you can use soft delete on a relationship!
– Ross Wilson
Aug 23 '15 at 7:30
True, just checked the code. Good to know for the future :)
– jedrzej.kurylo
Aug 23 '15 at 7:43
does it guarantee transaction @RossWilson
– Tomonso Ejang
Jul 20 at 8:07
|
show 3 more comments
up vote
9
down vote
up vote
9
down vote
You can go one of 2 ways with this.
The simplest way would be to override Eloquents delete()
method and include the related models as well e.g.:
public function delete()
{
$this->payments()->delete();
return parent::delete();
}
The above method should work just find but it seems a little bit dirty and I'd say it's not the preferred method within the community.
The cleaner way (IMO) would be to tap into Eloquents events e.g.:
public static function boot()
{
parent::boot();
static::deleting(function($invoice) {
$invoice->payments()->delete();
});
}
Either (but not both) of the above methods would go in your Invoice
model.
Also, I'm assuming that you have your relationships set up in your model, however, I'm not sure if you allow multiple payments for one invoice. Either way you might need to change the payments()
in the examples to whatever you've named the relationship in your invoice model.
Hope this helps!
You can go one of 2 ways with this.
The simplest way would be to override Eloquents delete()
method and include the related models as well e.g.:
public function delete()
{
$this->payments()->delete();
return parent::delete();
}
The above method should work just find but it seems a little bit dirty and I'd say it's not the preferred method within the community.
The cleaner way (IMO) would be to tap into Eloquents events e.g.:
public static function boot()
{
parent::boot();
static::deleting(function($invoice) {
$invoice->payments()->delete();
});
}
Either (but not both) of the above methods would go in your Invoice
model.
Also, I'm assuming that you have your relationships set up in your model, however, I'm not sure if you allow multiple payments for one invoice. Either way you might need to change the payments()
in the examples to whatever you've named the relationship in your invoice model.
Hope this helps!
edited Aug 23 '15 at 7:00
answered Aug 23 '15 at 6:35
Ross Wilson
15.3k22539
15.3k22539
Calling delete() on payments relation directly will bypass the model and won't trigger the SoftDelete on related models. They will be removed from the database. In order to make thek soft-deleted you need to call delete() on each of related models.
– jedrzej.kurylo
Aug 23 '15 at 6:38
You are also missing the return statement in your overridden delete method - it should do "return parent::delete();", otherwise you lose the value that would be returned from delete() if you hadn't overwritten it.
– jedrzej.kurylo
Aug 23 '15 at 6:54
@jedrzej.kurylo, I've just tested to make sure and YES you can use soft delete on a relationship!
– Ross Wilson
Aug 23 '15 at 7:30
True, just checked the code. Good to know for the future :)
– jedrzej.kurylo
Aug 23 '15 at 7:43
does it guarantee transaction @RossWilson
– Tomonso Ejang
Jul 20 at 8:07
|
show 3 more comments
Calling delete() on payments relation directly will bypass the model and won't trigger the SoftDelete on related models. They will be removed from the database. In order to make thek soft-deleted you need to call delete() on each of related models.
– jedrzej.kurylo
Aug 23 '15 at 6:38
You are also missing the return statement in your overridden delete method - it should do "return parent::delete();", otherwise you lose the value that would be returned from delete() if you hadn't overwritten it.
– jedrzej.kurylo
Aug 23 '15 at 6:54
@jedrzej.kurylo, I've just tested to make sure and YES you can use soft delete on a relationship!
– Ross Wilson
Aug 23 '15 at 7:30
True, just checked the code. Good to know for the future :)
– jedrzej.kurylo
Aug 23 '15 at 7:43
does it guarantee transaction @RossWilson
– Tomonso Ejang
Jul 20 at 8:07
Calling delete() on payments relation directly will bypass the model and won't trigger the SoftDelete on related models. They will be removed from the database. In order to make thek soft-deleted you need to call delete() on each of related models.
– jedrzej.kurylo
Aug 23 '15 at 6:38
Calling delete() on payments relation directly will bypass the model and won't trigger the SoftDelete on related models. They will be removed from the database. In order to make thek soft-deleted you need to call delete() on each of related models.
– jedrzej.kurylo
Aug 23 '15 at 6:38
You are also missing the return statement in your overridden delete method - it should do "return parent::delete();", otherwise you lose the value that would be returned from delete() if you hadn't overwritten it.
– jedrzej.kurylo
Aug 23 '15 at 6:54
You are also missing the return statement in your overridden delete method - it should do "return parent::delete();", otherwise you lose the value that would be returned from delete() if you hadn't overwritten it.
– jedrzej.kurylo
Aug 23 '15 at 6:54
@jedrzej.kurylo, I've just tested to make sure and YES you can use soft delete on a relationship!
– Ross Wilson
Aug 23 '15 at 7:30
@jedrzej.kurylo, I've just tested to make sure and YES you can use soft delete on a relationship!
– Ross Wilson
Aug 23 '15 at 7:30
True, just checked the code. Good to know for the future :)
– jedrzej.kurylo
Aug 23 '15 at 7:43
True, just checked the code. Good to know for the future :)
– jedrzej.kurylo
Aug 23 '15 at 7:43
does it guarantee transaction @RossWilson
– Tomonso Ejang
Jul 20 at 8:07
does it guarantee transaction @RossWilson
– Tomonso Ejang
Jul 20 at 8:07
|
show 3 more comments
up vote
4
down vote
I know you asked this question a long time ago but I found this package to be very simple and straightforward:
https://github.com/Askedio/laravel5-soft-cascade
Or you can use this package it's useful too:
https://github.com/michaeldyrynda/laravel-cascade-soft-deletes
Remember to install the right version depending on your laravel version.
You must install it via composer:
composer require askedio/laravel5-soft-cascade ^version
In second package:
composer require iatstuti/laravel-cascade-soft-deletes
Register the service provider in your config/app.php.
you can read the docs on the GitHub page.
If you delete a record this package recognizes all of its children and soft-delete them as well.
If you have another relationship in your child model use its trait in that model as well. its so much easier than doing it manually.
The second package has the benefit of deleting grandchildren of the model. in some cases, I say its a better approach.
add a comment |
up vote
4
down vote
I know you asked this question a long time ago but I found this package to be very simple and straightforward:
https://github.com/Askedio/laravel5-soft-cascade
Or you can use this package it's useful too:
https://github.com/michaeldyrynda/laravel-cascade-soft-deletes
Remember to install the right version depending on your laravel version.
You must install it via composer:
composer require askedio/laravel5-soft-cascade ^version
In second package:
composer require iatstuti/laravel-cascade-soft-deletes
Register the service provider in your config/app.php.
you can read the docs on the GitHub page.
If you delete a record this package recognizes all of its children and soft-delete them as well.
If you have another relationship in your child model use its trait in that model as well. its so much easier than doing it manually.
The second package has the benefit of deleting grandchildren of the model. in some cases, I say its a better approach.
add a comment |
up vote
4
down vote
up vote
4
down vote
I know you asked this question a long time ago but I found this package to be very simple and straightforward:
https://github.com/Askedio/laravel5-soft-cascade
Or you can use this package it's useful too:
https://github.com/michaeldyrynda/laravel-cascade-soft-deletes
Remember to install the right version depending on your laravel version.
You must install it via composer:
composer require askedio/laravel5-soft-cascade ^version
In second package:
composer require iatstuti/laravel-cascade-soft-deletes
Register the service provider in your config/app.php.
you can read the docs on the GitHub page.
If you delete a record this package recognizes all of its children and soft-delete them as well.
If you have another relationship in your child model use its trait in that model as well. its so much easier than doing it manually.
The second package has the benefit of deleting grandchildren of the model. in some cases, I say its a better approach.
I know you asked this question a long time ago but I found this package to be very simple and straightforward:
https://github.com/Askedio/laravel5-soft-cascade
Or you can use this package it's useful too:
https://github.com/michaeldyrynda/laravel-cascade-soft-deletes
Remember to install the right version depending on your laravel version.
You must install it via composer:
composer require askedio/laravel5-soft-cascade ^version
In second package:
composer require iatstuti/laravel-cascade-soft-deletes
Register the service provider in your config/app.php.
you can read the docs on the GitHub page.
If you delete a record this package recognizes all of its children and soft-delete them as well.
If you have another relationship in your child model use its trait in that model as well. its so much easier than doing it manually.
The second package has the benefit of deleting grandchildren of the model. in some cases, I say its a better approach.
edited May 12 at 12:48
answered Apr 18 at 12:50
Salar Bahador
4251313
4251313
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32163509%2fhow-to-soft-delete-related-records-when-soft-deleting-a-parent-record-in-laravel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown