The update method returns the number of affected rows. The update method expects an array of column and value pairs representing the columns that should be updated. You may use the make:model Artisan command to generate a new model: Models typically live in the app\Models directory and extend the Illuminate\Database\Eloquent\Model class. To get started, let's create an Eloquent model. It's a great way to get a tour of everything that Laravel and Eloquent have to offer. The Laravel Bootcamp will walk you through building your first Laravel application using Eloquent. If you're new to Laravel, feel free to jump into the Laravel Bootcamp. For more information on configuring your database, check out the database configuration documentation. In addition to retrieving records from the database table, Eloquent models allow you to insert, update, and delete records from the table as well.īefore getting started, be sure to configure a database connection in your application's config/database.php configuration file. When using Eloquent, each database table has a corresponding "Model" that is used to interact with that table. If you don't particularly like that approach, you will need to iterate your collection of organization products and call delete() on them individually.Laravel includes Eloquent, an object-relational mapper (ORM) that makes it enjoyable to interact with your database. now this will only destroy ids associated with the org $orgIds = array_intersect($org->products()->lists('id'), $ids) intersect the product ids for the org with those passed in As you mention in a comment, it won't restrict the deletion to only those products in the organization, so you would need to filter out those ids before passing the list into the destroy() method. 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. Now this, from what I can see, should have been simple.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |