2

need some help please

when i delete a campaign form my crowdfunding platform i got a error if this campaign have already some payments ,

the payments still in the database without campaign so i got the error in my payments list view . my only solution is to go to database and delete payments that is belong to the deleted campaign.

when i delete campaign , it must also delete his own payments

  • campaign Controller (delete function) :
    public function deleteCampaigns($id = 0){
        if(config('app.is_demo')){
            return redirect()->back()->with('error', __('app.feature_disable_demo'));
        }

        if ($id){
            $campaign = Campaign::find($id);
            if ($campaign){
                $campaign->delete();
            }
        }
        return back()->with('success', trans('app.campaign_deleted'));
    }

every payments in database is related to a campaign_id from campaign table

2 Answers2

2

If you have the toMany relationship properly set up on the Campaign model, then you can just call delete() on that relationship and it should delete all the related payment provided there is no additional constraint.

So assuming the toMany relationship to payment is called payments then:

if ($campaign){
  $campaign->payments()->delete();
  $campaign->delete();
}

EDIT:

As per what Alex Mac mentions in the comment, this may or may not be the best solution for your issue although i believe it is the most straightforward answer. Have a read on the answers on this Question

Helioarch
  • 1,158
  • 5
  • 18
  • This works. However, I would highly recommend structuring your migrations better. That way you don't have to remember to delete every relationship every time you need to delete a campaign. You are opening the doors for other developers to potentially misuse your models and screw up the database. – amac Feb 21 '20 at 04:38
  • 1
    @AlexMac I agree that there are other ways as you mention however there also concerns that can be raised for that method, for example if the OP is actually soft deleting his model then cascade deletion through migration will not work. I've edited my answer to show the discussions in another question post. – Helioarch Feb 21 '20 at 05:15
  • there are plenty of packages that do soft delete on cascade. However, the OP did not mention that and https://laravel-news.com/cascading-soft-deletes. Therefore, yes depending on your use case use the right tool. I would then recommend some function or method to handle that case in one line. Rather than allowing a potential misues. – amac Feb 21 '20 at 05:17
  • 1
    Soft delete is actually natively supported by laravel as per the [Documentation](https://laravel.com/docs/6.x/eloquent#soft-deleting) and if you read through the answer and comments on the link on my edited answer some people also brought up that the cascading delete is actually not supported by some DBs. So, although i agree that there is potential misuse, suggesting to do cascading deletion might just simply not work depending on the OP's setup. – Helioarch Feb 21 '20 at 05:27
  • Actually SoftDelete on cascade in not supported out of the box... Key word on cascade softdeleting a model has existed since 5.0 or before ;) – amac Feb 21 '20 at 05:28
  • 1
    That's my point, but soft delete IS supported by Laravel out of the box. That's why since we don't know more information on the OP setup, i just gave the straighforward answer and after your comment redirect them to other question for the full discussion on this topic and let them decide :) – Helioarch Feb 21 '20 at 05:36
0

You could create a foreign key constraint in your migration. For example,

$table->foreign('campaign_id') ->references('id')->on('payments') ->onDelete('cascade');

So when you delete a campaign it will delete its payment foreign key constraint model or easier said its eloquent hasMany relationship.

amac
  • 921
  • 1
  • 6
  • 17