php - How to delete multiple records using Laravel Eloquent -


now this, can see, should have been simple.

i want able delete multiple records database. have id's of records wish delete. call resource.destroy route using comma separated list of ids (id of postgres type uuid), so:

request url:http://foo.app/products/62100dd6-7ecf-4870-aa79-4b132e60c904,c4b369f1-d1ef-4aa2-b4df-b9bc300a4ff5 request method:delete 

on other end, controller action looks so:

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

this gives me following error:

badmethodcallexception in macroable.php line 81: method delete not exist.  in macroable.php line 81 @ collection->__call('delete', array()) in productscontroller.php line 251 @ collection->delete() in productscontroller.php line 251 @ productscontroller->destroy('62100dd6-7ecf-4870-aa79-4b132e60c904,c4b369f1-d1ef-4aa2-b4df-b9bc300a4ff5') 

i have verified find() returning collection of products matching specified ids.

what missing?

ps: 1. model product has several belongsto relationships other models. 2. product.destroy code works fine if pass single id

edit guess, i'm trying understand difference between:

$org->products()->find($ids)->delete() 

and

$org->products()->wherein('id', $ids)->get()->delete() 

is? see, both find , get returning collections

the issue you're calling delete() on collection, not have method.

you have couple options here.

model events

if have event listeners deleting/deleted model events, need make sure deletion happens in way each model loaded , deleted.

in case, can use destroy method on model takes list of ids. load new model each id, , call delete() on it. mention in comment, won't restrict deletion products in organization, need filter out ids before passing list destroy() method.

public function destroy($id) {     try {         $ids = explode(",", $id);         // intersect product ids org passed in         $orgids = array_intersect($org->products()->lists('id'), $ids);         // destroy ids associated org         \app\product::destroy($orgids);     }     catch(...) {     } } 

if don't particularly approach, need iterate collection of organization products , call delete() on them individually. can use standard foreach, or can use each method on collection:

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

no model events

now, if don't have model events need listen for, things little easier. in case, can call delete() on query builder, , go straight deleting records without loading model objects. so, cleaner code better performance:

public function destroy($id) {     try {         $ids = explode(",", $id);         // call delete on query builder (no get())         $org->products()->wherein('id', $ids)->delete();     }     catch(...) {     } } 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -