1"use strict";(self.webpackChunkfirestore_eloquent=self.webpackChunkfirestore_eloquent||[]).push([[7488],{1970:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>o,contentTitle:()=>s,default:()=>u,frontMatter:()=>a,metadata:()=>r,toc:()=>l});var d=n(5893),i=n(1151);const a={sidebar_position:3},s="Updating data",r={id:"guide/eloquent/updating-data",title:"Updating data",description:"Updating Documents in Firestore Eloquent Library",source:"@site/versioned_docs/version-1.x/guide/eloquent/updating-data.mdx",sourceDirName:"guide/eloquent",slug:"/guide/eloquent/updating-data",permalink:"/docs/1.x/guide/eloquent/updating-data",draft:!1,unlisted:!1,editUrl:"https://github.com/FreddyWhest/firestore-eloquent-docs/tree/main/versioned_docs/version-1.x/guide/eloquent/updating-data.mdx",tags:[],version:"1.x",sidebarPosition:3,frontMatter:{sidebar_position:3},sidebar:"tutorialSidebar",previous:{title:"Retrieving data",permalink:"/docs/1.x/guide/eloquent/retrieving-data"},next:{title:"Create Document",permalink:"/docs/1.x/guide/eloquent/create"}},o={},l=[{value:"Updating Documents in Firestore Eloquent Library",id:"updating-documents-in-firestore-eloquent-library",level:2},{value:"Update Many Documents with <code>updateMany()</code>",id:"update-many-documents-with-updatemany",level:2},{value:"Basic Update",id:"basic-update",level:3},{value:"Update Multiple Fields",id:"update-multiple-fields",level:3},{value:"Partial Update with <code>update()</code>",id:"partial-update-with-update",level:2},{value:"Basic Update",id:"basic-update-1",level:3},{value:"Update Multiple Fields",id:"update-multiple-fields-1",level:3}];function c(e){const t={admonition:"admonition",code:"code",h1:"h1",h2:"h2",h3:"h3",p:"p",pre:"pre",...(0,i.a)(),...e.components};return(0,d.jsxs)(d.Fragment,{children:[(0,d.jsx)(t.h1,{id:"updating-data",children:"Updating data"}),"\n",(0,d.jsx)(t.h2,{id:"updating-documents-in-firestore-eloquent-library",children:"Updating Documents in Firestore Eloquent Library"}),"\n",(0,d.jsx)(t.p,{children:"Updating documents in Firestore is a common operation, and the Firestore Eloquent Library in Laravel provides convenient methods to perform updates. Let's explore how to update documents using the library's features."}),"\n",(0,d.jsxs)(t.h2,{id:"update-many-documents-with-updatemany",children:["Update Many Documents with ",(0,d.jsx)(t.code,{children:"updateMany()"})]}),"\n",(0,d.jsx)(t.admonition,{type:"warning",children:(0,d.jsx)(t.p,{children:"Updating many documents in a collection can take a long time if there are a lot of documents in the sub collection."})}),"\n",(0,d.jsxs)(t.p,{children:["The ",(0,d.jsx)(t.code,{children:"updateMany()"})," method allows you to update multiple documents in a Firestore collection based on a specified query. You can use this method to update fields of documents that match certain conditions."]}),"\n",(0,d.jsx)(t.h3,{id:"basic-update",children:"Basic Update"}),"\n",(0,d.jsxs)(t.p,{children:["Let's update the ",(0,d.jsx)(t.code,{children:"age"})," field of all documents in the ",(0,d.jsx)(t.code,{children:"users"})," collection that have a ",(0,d.jsx)(t.code,{children:"role"})," field with the value ",(0,d.jsx)(t.code,{children:"admin"}),"."]}),"\n",(0,d.jsx)(t.pre,{children:(0,d.jsx)(t.code,{className:"language-php",children:"// Update all users with age 20 to age 25\r\nUser::where(['role', '=', 'admin'])->updateMany(['age' => 25]);\n"})}),"\n",(0,d.jsx)(t.h3,{id:"update-multiple-fields",children:"Update Multiple Fields"}),"\n",(0,d.jsxs)(t.p,{children:["You can also update multiple fields of documents that match a query. Let's update the ",(0,d.jsx)(t.code,{children:"name"})," and ",(0,d.jsx)(t.code,{children:"age"})," fields of all documents in the ",(0,d.jsx)(t.code,{children:"users"})," collection that have a ",(0,d.jsx)(t.code,{children:"role"})," field with the value ",(0,d.jsx)(t.code,{children:"admin"}),"."]}),"\n",(0,d.jsx)(t.pre,{children:(0,d.jsx)(t.code,{className:"language-php",children:"// Update all users with age 20 to age 25\r\nUser::where(['role', '=', 'admin'])->updateMany(['age' => 25, 'name' => 'John Doe']);\n"})}),"\n",(0,d.jsxs)(t.h2,{id:"partial-update-with-update",children:["Partial Update with ",(0,d.jsx)(t.code,{children:"update()"})]}),"\n",(0,d.jsxs)(t.p,{children:["The ",(0,d.jsx)(t.code,{children:"update()"})," method allows you to update a single document in a Firestore collection. You can use this method to update fields of a document that match certain conditions."]}),"\n",(0,d.jsx)(t.h3,{id:"basic-update-1",children:"Basic Update"}),"\n",(0,d.jsxs)(t.p,{children:["Let's update the ",(0,d.jsx)(t.code,{children:"age"})," field of the document with the ",(0,d.jsx)(t.code,{children:"id"})," ",(0,d.jsx)(t.code,{children:"1234"})," in the ",(0,d.jsx)(t.code,{children:"users"})," collection."]}),"\n",(0,d.jsx)(t.pre,{children:(0,d.jsx)(t.code,{className:"language-php",children:"// Update a user with the specified ID\r\n$userId = '1234';\r\n$user = User::find($userId);\r\n$updatedUser = $user->update([\r\n 'age' => 30\r\n]);\r\n\r\necho $updatedUser->age; // 30\r\n\n"})}),"\n",(0,d.jsx)(t.h3,{id:"update-multiple-fields-1",children:"Update Multiple Fields"}),"\n",(0,d.jsxs)(t.p,{children:["You can also update multiple fields of a document. Let's update the ",(0,d.jsx)(t.code,{children:"status"})," and ",(0,d.jsx)(t.code,{children:"age"})," fields of the document with the ",(0,d.jsx)(t.code,{children:"id"})," ",(0,d.jsx)(t.code,{children:"1234"})," in the ",(0,d.jsx)(t.code,{children:"users"})," collection."]}),"\n",(0,d.jsx)(t.pre,{children:(0,d.jsx)(t.code,{className:"language-php",children:"// Update a user with the specified ID\r\n// Update a user with the specified ID\r\n$user = User::where(['user_id', '=', $userId])->first();\r\n$updatedUser = $user->update([\r\n 'age' => 30, \r\n 'status' => 'inactive'\r\n]);\r\n\r\necho $updatedUser->age; // 30\r\necho $updatedUser->status; // inactive\r\n\n"})}),"\n",(0,d.jsx)(t.pre,{children:(0,d.jsx)(t.code,{className:"language-php",children:"// Update a user with the specified ID\r\n$user = User::where(['user_id', '=', $userId])->firstOrFail();\r\n$updatedUser = $user->update([\r\n 'age' => 30, \r\n 'status' => 'inactive'\r\n]);\r\n\r\necho $updatedUser->age; // 30\r\necho $updatedUser->status; // inactive\r\n\n"})})]})}function u(e={}){const{wrapper:t}={...(0,i.a)(),...e.components};return t?(0,d.jsx)(t,{...e,children:(0,d.jsx)(c,{...e})}):c(e)}},1151:(e,t,n)=>{n.d(t,{Z:()=>r,a:()=>s});var d=n(7294);const i={},a=d.createContext(i);function s(e){const t=d.useContext(a);return d.useMemo((function(){return"function"==typeof e?e(t):{...t,...e}}),[t,e])}function r(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:s(e.components),d.createElement(a.Provider,{value:t},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.