Laravel - updateOrCreate or a sync method?

inuShiva

I'm trying to see what my best route should be but for the sake of ease I will make this sound much simpler than it is.

I have two models, one called donor and one called donation. For the relationships, the donor has many donations.

In my edit form of the donor model, I have the ability to create, update or delete the donations from the same form.

Updating and creating are easy at the moment because I can use updateOrCreate. But what happens if I want to delete a donation?

Would I perform an actual query filtering out the ids of the donations that were still on the edit form (and therefore not deleted by the user) and then delete the models from there? Or is there a better way of handling this action?

Thanks in advance.

Piazzi

In your DonationController you could select the donations you want to delete with a query like this:

public function destroy($id)
{

 $donation = Donation::where('donor_id','=', $id)->get();
 $donation->delete();

}

In your view you could make a delete form and them send him to this function in the controller. I think the best way to do it is make a foreach and loop the donations from the donor inside.

@foreach($donor->donations as $donation)
{
  <form method="POST" action="{{route('donor.destroy', $donation->id)}}">
  <button type="submit">Delete {{$donation->id}}</button>   
  </form>
}
@endforeach

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related