mercredi 27 janvier 2016

How to delete multiple records using Laravel Eloquent

Now this, from what I can see, should have been simple.

I want to be able to delete multiple records from the database. I have the id's of all the records I wish to delete. I call the resource.destroy route using comma separated list of ids (id is of postgres type uuid), like so:

Request URL:http://ift.tt/20taZ3g
Request Method:DELETE

On the other end, my controller action looks like so:

public function destroy($id)
{
    try {
        $ids = explode(",", $id);
        $org->products()->find($ids)->delete();
    }
    catch(...) {
    }
}

This gives me the following error:

BadMethodCallException in Macroable.php line 81:
Method delete does not exist.

in Macroable.php line 81
at Collection->__call('delete', array()) in ProductsController.php line 251
at Collection->delete() in ProductsController.php line 251
at ProductsController->destroy('62100dd6-7ecf-4870-aa79-4b132e60c904,c4b369f1-d1ef-4aa2-b4df-b9bc300a4ff5')

I have verified that find() is returning a collection of products matching the specified ids.

What am I missing?

PS: 1. The model Product has several belongsTo relationships with other models. 2. The product.destroy code works fine if I pass it a single id



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire