It will load a new model for each id, and then call delete() on it. In this case, you can use the destroy method on the model that takes a list of ids. If you have event listeners for the deleting/ deleted model events, you will need to make sure the deletion happens in a way that each model is loaded and then deleted. The issue is that you're calling delete() on a Collection, which does not have that method. Is? From what I see, both find and get are returning Collections I guess, I'm also trying to understand what the difference between: $org->products()->find($ids)->delete()Īnd $org->products()->whereIn('id', $ids)->get()->delete() The stroy code works fine if I pass it a single id The model Product has several belongsTo relationships with other models.Ģ. I have verified that find() is returning a collection of products matching the specified ids.ġ. This gives me the following error: BadMethodCallException in Macroable.php line 81:Īt Collection->_call('delete', array()) in ProductsController.php line 251Īt Collection->delete() in ProductsController.php line 251Īt ProductsController->destroy('62100dd6-7ecf-4870-aa79-4b132e60c904,c4b369f1-d1ef-4aa2-b4df-b9bc300a4ff5') On the other end, my controller action looks like so: public function destroy($id) I call the stroy route using comma separated list of ids ( id is of postgres type uuid), like so: Request URL: I have the id's of all the records I wish to delete. I want to be able to delete multiple records from the database. If you want to learn more about PHP in general, make sure to check out this free PHP basics course here.Now this, from what I can see, should have been simple. If you are just getting started with Laravel, make sure to check out this introduction course here: The main benefit of using turncate is that the auto-increment IDs will also be rest. The benefit of using delete() rather than using truncate() is that you could specify some conditions so that you could only delete specific entries and not all of them: Post::where( 'active', false)->delete() Īlternatively, you could also use the DB facade rather than calling the model directly: DB::table( 'posts')->delete() ĭB::table( 'posts')->where( 'active', false)->delete() If you wanted to delete only some specific entries, you could first get the entries as follows: Post::query()->delete() To do the same thing with Eloquent, we could use the DB facade as follows: DB::table( 'posts')->truncate() Īlternatively, you could call the truncate() method directly on your model: Post ::truncate() ĭelete all entries using the delete() method This will basically truncate the entire table, and it would also reset the auto-incrementing IDs to zero. Learn more about the DevDojo sponsorship program and see your logo here to get your brand in front of thousands of developers. View Website Learn how to code your own blockchain and create your own crypto-currency with the CoinCamp interactive and fun online training platform. If we were writing pure SQL, to delete all entries from a table, we could use the TRUNCATE function as follows: Using truncate to delete all entries from a table We will use a model called Post as an example in this tutorial. Or you could use this awesome script to do the installation:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |