5757 This is just to show the code looks identical to the MongoDB version
5858*/
5959Route::get ('/create_eloquent_sql/ ' , function (Request $ request ) {
60- $ success = CustomerSQL::create ([
61- 'guid ' => 'cust_0000 ' ,
62- 'first_name ' => 'John ' ,
63- 'family_name ' => 'Doe ' ,
64- 'email ' => 'j.doe@gmail.com ' ,
65- 'address ' => '123 my street, my city, zip, state, country '
66- ]);
60+
61+ try {
62+ $ success = CustomerSQL::create ([
63+ 'guid ' => 'cust_0000 ' ,
64+ 'first_name ' => 'John ' ,
65+ 'family_name ' => 'Doe ' ,
66+ 'email ' => 'j.doe@gmail.com ' ,
67+ 'address ' => '123 my street, my city, zip, state, country '
68+ ]);
69+ $ msg = "OK " ;
70+ }
71+ catch (\Exception $ e ) {
72+ $ msg = 'Create user via Eloquent SQL model. Error: ' . $ e ->getMessage ();
73+ }
6774
68- return ['msg ' => 'executed ' , 'data ' => $ success ];
75+ return ['status ' => 'executed ' , 'msg ' => $ msg ];
6976});
7077
7178/*
7279 Create a new "customer" in our SQL database
7380 This is just to show the code looks identical to the MongoDB version
7481*/
7582Route::get ('/create_eloquent_mongo/ ' , function (Request $ request ) {
76- $ success = CustomerMongoDB::create ([
77- 'guid ' => 'cust_1111 ' ,
78- 'first_name ' => 'John ' ,
79- 'family_name ' => 'Doe ' ,
80- 'email ' => 'j.doe@gmail.com ' ,
81- 'address ' => '123 my street, my city, zip, state, country '
82- ]);
83-
84- return ['msg ' => 'executed ' , 'data ' => $ success ];
83+ try {
84+ $ success = CustomerMongoDB::create ([
85+ 'guid ' => 'cust_1111 ' ,
86+ 'first_name ' => 'John ' ,
87+ 'family_name ' => 'Doe ' ,
88+ 'email ' => 'j.doe@gmail.com ' ,
89+ 'address ' => '123 my street, my city, zip, state, country '
90+ ]);
91+ $ msg = "OK " ;
92+ }
93+ catch (\Exception $ e ) {
94+ $ msg = 'Create user via Eloquent MongoDB model. Error: ' . $ e ->getMessage ();
95+ }
96+
97+ return ['status ' => 'executed ' , 'data ' => $ msg ];
8598});
8699
87100/*
91104
92105 $ customer = CustomerMongoDB::where ('guid ' , 'cust_1111 ' )->get ();
93106
94- return ['msg ' => 'executed ' , 'data ' => $ customer ];
107+ return ['status ' => 'executed ' , 'data ' => $ customer ];
95108});
96109
97110/*
98111 Update a record using Eloquent + MongoDB
99112*/
100113Route::get ('/update_eloquent/ ' , function (Request $ request ) {
101- $ result = CustomerMongoDB::where ('guid ' , 'cust_1111 ' )->update (['first_name ' , 'Jimmy ' ]);
114+ $ result = CustomerMongoDB::where ('guid ' , 'cust_1111 ' )->update ( ['first_name ' => 'Jimmy ' ] );
102115
103- return ['msg ' => 'executed ' , 'data ' => $ result ];
116+ return ['status ' => 'executed ' , 'data ' => $ result ];
104117});
105118
106119/*
107120 Delete a record using Eloquent + MongoDB
108121*/
109122Route::get ('/delete_eloquent/ ' , function (Request $ request ) {
110- $ result = CustomerMongoDB::destroy ('guid ' , 'cust_1111 ' );
123+ $ result = CustomerMongoDB::where ('guid ' , 'cust_1111 ' )-> delete ( );
111124
112- return ['msg ' => 'executed ' , 'data ' => $ result ];
125+ return ['status ' => 'executed ' , 'data ' => $ result ];
113126});
114127
115128/*
139152 $ message = $ e ->getMessage ();
140153 }
141154
142- return ['msg ' => $ message , 'data ' => $ success ];
155+ return ['status ' => $ message , 'data ' => $ success ];
143156});
144157
145158
194207 $ cust_array [] = $ cust ->newFromBuilder ( $ bson );
195208 }
196209
197- return ['msg ' => 'executed ' , 'whereraw ' => $ results , 'document ' => $ one_doc , 'cursor_array ' => $ cust_array ];
210+ return ['status ' => 'executed ' , 'whereraw ' => $ results , 'document ' => $ one_doc , 'cursor_array ' => $ cust_array ];
198211});
199212
200213/*
207220 $ update = ['$set ' => ['first_name ' => 'Henry ' , 'address.street ' => '777 new street name ' ] ];
208221 $ result = $ mdb_collection ->updateOne ($ match , $ update );
209222
210- return ['msg ' => 'executed ' , 'matched_docs ' => $ result ->getMatchedCount (), 'modified_docs ' => $ result ->getModifiedCount ()];
223+ return ['status ' => 'executed ' , 'matched_docs ' => $ result ->getMatchedCount (), 'modified_docs ' => $ result ->getModifiedCount ()];
211224});
212225
213226/*
219232 $ match = ['guid ' => 'cust_2222 ' ];
220233 $ result = $ mdb_collection ->deleteOne ( $ match );
221234
222- return ['msg ' => 'executed ' , 'deleted_docs ' => $ result ->getDeletedCount () ];
235+ return ['status ' => 'executed ' , 'deleted_docs ' => $ result ->getDeletedCount () ];
223236});
224237
225238/*
237250
238251 $ mdb_cursor = $ mdb_collection ->aggregate ( $ aggregation );
239252
240- return ['msg ' => 'executed ' , 'data ' => $ mdb_cursor ->toArray () ];
253+ return ['status ' => 'executed ' , 'data ' => $ mdb_cursor ->toArray () ];
241254});
242255
243256/*
249262 $ indexOptions = ["unique " => true ];
250263 $ result = DB ::connection ('mongodb ' )->getCollection ('laracoll ' )->createIndex ($ indexKeys , $ indexOptions );
251264
252- return ['msg ' => 'executed ' , 'data ' => $ result ];
253- });
254-
255-
256- /*
257- TEMPORARY endpoint to test various snippets
258- */
259- Route::get ('/mongodb_api/ ' , function (Request $ request ) {
260-
261- $ address = new stdClass ;
262- $ address ->street = '123 my street name ' ;
263- $ address ->city = 'my city ' ;
264- $ address ->zip = '12345 ' ;
265-
266- $ emails = array ( 'j.doe@gmail.com ' ,'j.doe@work.com ' );
267-
268- $ customer = new CustomerMongoDB ();
269- $ customer ->guid = 'cust_1111 ' ;
270- $ customer ->first_name = 'John ' ;
271- $ customer ->family_name = 'Doe ' ;
272- $ customer ->email = $ emails ;
273- $ customer ->address = $ address ;
274-
275-
276- $ collectionName = $ customer ->getTable ();
277-
278- // ❌ what's the BEST WAY to access the native MongoDB collection object?
279- // getting it from the Model would be best since the Model knows about the connection AND the collection.
280- // but I don't see anything to achieve that
281-
282- // this insertOne() works well with an stdClass, but ❌ not with a Model (use Model::save() instead)
283- //
284- // $result = DB::connection('mongodb')->getCollection('laracoll')->insertOne( $address );
285-
286- // Create an index with a primary key
287- /* $indexKeys = ["guid" => 1];
288- $indexOptions = ["unique" => true];
289- $result = DB::connection('mongodb')->getCollection('laracoll')->createIndex($indexKeys, $indexOptions); */
290-
291- //
292- $ query = ['guid ' => 'cust_1111 ' , ];
293- $ cursor = DB ::connection ('mongodb ' )->getCollection ('laracoll ' )->find ( $ query );
294- $ result = $ cursor ->toArray ();
295-
296- /*
297- // ❌ works, but not very convenient to pass parameters, data
298- $result = CustomerMongoDB::raw(function (Collection $collection) {
299- return $collection->insertOne( SOME DATA );
300- }); */
301-
302-
303- // aggregation pipeline
304- /* collection->aggregate($pipeline, $options)); */
305-
306- $ resp = new stdClass ;
307- $ resp ->msg = "executed " ;
308- $ resp ->data = $ result ;
309- return $ resp ;
310- });
311-
265+ return ['status ' => 'executed ' , 'data ' => $ result ];
266+ });
0 commit comments