Skip to content

Commit 5575dcf

Browse files
committed
Clean up
- made sure all API routes work. fixed a syntax issue found in the update code - add try{} blocks where useful for the tutorial - remove unused temporary code
1 parent 3ac989f commit 5575dcf

File tree

2 files changed

+46
-93
lines changed

2 files changed

+46
-93
lines changed

app/Models/CustomerMongoDB.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ class CustomerMongoDB extends Model
1111
{
1212
use HasFactory;
1313

14+
// the selected database as defined in /config/database.php
1415
protected $connection = 'mongodb';
15-
protected $collection = 'laracoll'; // equivalent to $table for MySQL
1616

17-
protected $primaryKey = 'guid';
17+
// equivalent to $table for MySQL
18+
protected $collection = 'laracoll';
1819

1920
// defines the schema's top-level properties.
20-
// ❌ skip nested definitions?
21-
// each of these can be assgined as an object, array, array of objects etc
22-
protected $fillable = ['guid','first_name', 'family_name', 'email', 'address'];
23-
21+
protected $fillable = ['guid','first_name', 'family_name', 'email', 'address'];
2422
}

routes/api.php

Lines changed: 42 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,44 @@
5757
This is just to show the code looks identical to the MongoDB version
5858
*/
5959
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+
}
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
*/
7582
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];
8598
});
8699

87100
/*
@@ -91,25 +104,25 @@
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
*/
100113
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'] );
102115

103-
return ['msg' => 'executed', 'data' => $result];
116+
return ['status' => 'executed', 'data' => $result];
104117
});
105118

106119
/*
107120
Delete a record using Eloquent + MongoDB
108121
*/
109122
Route::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
/*
@@ -139,7 +152,7 @@
139152
$message = $e->getMessage();
140153
}
141154

142-
return ['msg' => $message, 'data' => $success];
155+
return ['status' => $message, 'data' => $success];
143156
});
144157

145158

@@ -194,7 +207,7 @@
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
/*
@@ -207,7 +220,7 @@
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
/*
@@ -219,7 +232,7 @@
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
/*
@@ -237,7 +250,7 @@
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
/*
@@ -249,63 +262,5 @@
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

Comments
 (0)