1"use strict";(self.webpackChunkfirestore_eloquent=self.webpackChunkfirestore_eloquent||[]).push([[4388],{9851:(e,r,t)=>{t.r(r),t.d(r,{assets:()=>d,contentTitle:()=>a,default:()=>c,frontMatter:()=>s,metadata:()=>i,toc:()=>l});var n=t(5893),o=t(1151);const s={sidebar_position:10},a="toArray",i={id:"guide/eloquent/toArray",title:"toArray",description:"This method is recommended when working with APIs or InertiaJS. It converts the returned data to an array.",source:"@site/versioned_docs/version-2.x/guide/eloquent/toArray.mdx",sourceDirName:"guide/eloquent",slug:"/guide/eloquent/toArray",permalink:"/docs/2.x/guide/eloquent/toArray",draft:!1,unlisted:!1,editUrl:"https://github.com/FreddyWhest/firestore-eloquent-docs/tree/main/versioned_docs/version-2.x/guide/eloquent/toArray.mdx",tags:[],version:"2.x",sidebarPosition:10,frontMatter:{sidebar_position:10},sidebar:"tutorialSidebar",previous:{title:"Sub collection",permalink:"/docs/2.x/guide/eloquent/sub-collection"},next:{title:"Like Method",permalink:"/docs/2.x/guide/eloquent/like-method"}},d={},l=[{value:"Example with <strong><code>find()</code></strong> method",id:"example-with-find-method",level:2},{value:"Example with <strong><code>get()</code></strong> method and filtering",id:"example-with-get-method-and-filtering",level:2},{value:"Example with <strong><code>all()</code></strong> method",id:"example-with-all-method",level:2},{value:"Example with <strong><code>first()</code></strong> and <strong><code>firstOrFail()</code></strong> method",id:"example-with-first-and-firstorfail-method",level:2}];function h(e){const r={code:"code",h1:"h1",h2:"h2",p:"p",pre:"pre",strong:"strong",...(0,o.a)(),...e.components};return(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)(r.h1,{id:"toarray",children:"toArray"}),"\n",(0,n.jsx)(r.p,{children:"This method is recommended when working with APIs or InertiaJS. It converts the returned data to an array."}),"\n",(0,n.jsxs)(r.h2,{id:"example-with-find-method",children:["Example with ",(0,n.jsx)(r.strong,{children:(0,n.jsx)(r.code,{children:"find()"})})," method"]}),"\n",(0,n.jsx)(r.pre,{children:(0,n.jsx)(r.code,{className:"language-php",children:"To convert the returned data to an array, you can use the `toArray` method.\r\n\r\n```php\r\n$user = User::find(1);\r\n\r\n// Convert the user to an array\r\n$user->toArray();\n"})}),"\n",(0,n.jsxs)(r.h2,{id:"example-with-get-method-and-filtering",children:["Example with ",(0,n.jsx)(r.strong,{children:(0,n.jsx)(r.code,{children:"get()"})})," method and filtering"]}),"\n",(0,n.jsxs)(r.p,{children:["To convert the returned data to an array, you can use the ",(0,n.jsx)(r.code,{children:"toArray"})," method."]}),"\n",(0,n.jsx)(r.pre,{children:(0,n.jsx)(r.code,{className:"language-php",children:"\r\n$users = User::get();\r\n\r\n// Convert the users to an array\r\n$users->toArray();\r\n\r\n//Filtering\r\n$user = User::where(['id', \"=\", 1])->get();\r\n\r\n// Convert the user to an array\r\n$user->toArray();\n"})}),"\n",(0,n.jsxs)(r.h2,{id:"example-with-all-method",children:["Example with ",(0,n.jsx)(r.strong,{children:(0,n.jsx)(r.code,{children:"all()"})})," method"]}),"\n",(0,n.jsxs)(r.p,{children:["To convert the returned data to an array, you can use the ",(0,n.jsx)(r.code,{children:"toArray"})," method."]}),"\n",(0,n.jsx)(r.pre,{children:(0,n.jsx)(r.code,{className:"language-php",children:"\r\n$users = User::all();\r\n\r\n// Convert the users to an array\r\n$users->toArray();\n"})}),"\n",(0,n.jsxs)(r.h2,{id:"example-with-first-and-firstorfail-method",children:["Example with ",(0,n.jsx)(r.strong,{children:(0,n.jsx)(r.code,{children:"first()"})})," and ",(0,n.jsx)(r.strong,{children:(0,n.jsx)(r.code,{children:"firstOrFail()"})})," method"]}),"\n",(0,n.jsxs)(r.p,{children:["To convert the returned data to an array, you can use the ",(0,n.jsx)(r.code,{children:"toArray"})," method."]}),"\n",(0,n.jsx)(r.pre,{children:(0,n.jsx)(r.code,{className:"language-php",children:"\r\n$user = User::first();\r\n\r\n// Convert the user to an array\r\n$user->toArray();\r\n\r\n$user = User::firstOrFail();\r\n\r\n// Convert the user to an array\r\n$user->toArray();\r\n\r\n//Filtering\r\n$user = User::where(['id', \"=\", 1])->first();\r\n\r\n// Convert the user to an array\r\n$user->toArray();\r\n\r\n//Filtering\r\n$user = User::where(['id', \"=\", 1])->firstOrFail();\r\n\r\n// Convert the user to an array\r\n$user->toArray();\n"})})]})}function c(e={}){const{wrapper:r}={...(0,o.a)(),...e.components};return r?(0,n.jsx)(r,{...e,children:(0,n.jsx)(h,{...e})}):h(e)}},1151:(e,r,t)=>{t.d(r,{Z:()=>i,a:()=>a});var n=t(7294);const o={},s=n.createContext(o);function a(e){const r=n.useContext(s);return n.useMemo((function(){return"function"==typeof e?e(r):{...r,...e}}),[r,e])}function i(e){let r;return r=e.disableParentContext?"function"==typeof e.components?e.components(o):e.components||o:a(e.components),n.createElement(s.Provider,{value:r},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.