57
57
This is just to show the code looks identical to the MongoDB version
58
58
*/
59
59
Route::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
+ }
67
74
68
- return ['msg ' => 'executed ' , 'data ' => $ success ];
75
+ return ['status ' => 'executed ' , 'msg ' => $ msg ];
69
76
});
70
77
71
78
/*
72
79
Create a new "customer" in our SQL database
73
80
This is just to show the code looks identical to the MongoDB version
74
81
*/
75
82
Route::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 ];
85
98
});
86
99
87
100
/*
91
104
92
105
$ customer = CustomerMongoDB::where ('guid ' , 'cust_1111 ' )->get ();
93
106
94
- return ['msg ' => 'executed ' , 'data ' => $ customer ];
107
+ return ['status ' => 'executed ' , 'data ' => $ customer ];
95
108
});
96
109
97
110
/*
98
111
Update a record using Eloquent + MongoDB
99
112
*/
100
113
Route::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 ' ] );
102
115
103
- return ['msg ' => 'executed ' , 'data ' => $ result ];
116
+ return ['status ' => 'executed ' , 'data ' => $ result ];
104
117
});
105
118
106
119
/*
107
120
Delete a record using Eloquent + MongoDB
108
121
*/
109
122
Route::get ('/delete_eloquent/ ' , function (Request $ request ) {
110
- $ result = CustomerMongoDB::destroy ('guid ' , 'cust_1111 ' );
123
+ $ result = CustomerMongoDB::where ('guid ' , 'cust_1111 ' )-> delete ( );
111
124
112
- return ['msg ' => 'executed ' , 'data ' => $ result ];
125
+ return ['status ' => 'executed ' , 'data ' => $ result ];
113
126
});
114
127
115
128
/*
139
152
$ message = $ e ->getMessage ();
140
153
}
141
154
142
- return ['msg ' => $ message , 'data ' => $ success ];
155
+ return ['status ' => $ message , 'data ' => $ success ];
143
156
});
144
157
145
158
194
207
$ cust_array [] = $ cust ->newFromBuilder ( $ bson );
195
208
}
196
209
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 ];
198
211
});
199
212
200
213
/*
207
220
$ update = ['$set ' => ['first_name ' => 'Henry ' , 'address.street ' => '777 new street name ' ] ];
208
221
$ result = $ mdb_collection ->updateOne ($ match , $ update );
209
222
210
- return ['msg ' => 'executed ' , 'matched_docs ' => $ result ->getMatchedCount (), 'modified_docs ' => $ result ->getModifiedCount ()];
223
+ return ['status ' => 'executed ' , 'matched_docs ' => $ result ->getMatchedCount (), 'modified_docs ' => $ result ->getModifiedCount ()];
211
224
});
212
225
213
226
/*
219
232
$ match = ['guid ' => 'cust_2222 ' ];
220
233
$ result = $ mdb_collection ->deleteOne ( $ match );
221
234
222
- return ['msg ' => 'executed ' , 'deleted_docs ' => $ result ->getDeletedCount () ];
235
+ return ['status ' => 'executed ' , 'deleted_docs ' => $ result ->getDeletedCount () ];
223
236
});
224
237
225
238
/*
237
250
238
251
$ mdb_cursor = $ mdb_collection ->aggregate ( $ aggregation );
239
252
240
- return ['msg ' => 'executed ' , 'data ' => $ mdb_cursor ->toArray () ];
253
+ return ['status ' => 'executed ' , 'data ' => $ mdb_cursor ->toArray () ];
241
254
});
242
255
243
256
/*
249
262
$ indexOptions = ["unique " => true ];
250
263
$ result = DB ::connection ('mongodb ' )->getCollection ('laracoll ' )->createIndex ($ indexKeys , $ indexOptions );
251
264
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