@@ -12,175 +12,133 @@ Installation
12
12
13
13
Add the package to your ` composer.json ` or install manually.
14
14
15
- ``` yaml
16
- {
17
- " require " : {
18
- " jenssegers/mongodb " : " * "
15
+ {
16
+ "require": {
17
+ "jenssegers/mongodb ": "*"
18
+ }
19
19
}
20
- }
21
- ```
22
20
23
21
Run ` composer update ` to download and install the package.
24
22
25
23
Add the service provider in ` app/config/app.php ` :
26
24
27
- ``` php
28
- 'Jenssegers\Mongodb\MongodbServiceProvider',
29
- ```
25
+ 'Jenssegers\Mongodb\MongodbServiceProvider',
30
26
31
27
Usage
32
28
-----
33
29
34
30
Tell your model to use the MongoDB model and a MongoDB collection (alias for table):
35
-
36
- ``` php
37
- use Jenssegers\Mongodb\Model as Eloquent
38
-
39
- class MyModel extends Eloquent {
40
-
41
- protected $collection = 'mycollection';
42
-
43
- }
44
- ```
31
+
32
+ use Jenssegers\Mongodb\Model as Eloquent
33
+
34
+ class MyModel extends Eloquent {
35
+
36
+ protected $collection = 'mycollection';
37
+
38
+ }
45
39
46
40
Configuration
47
41
-------------
48
42
49
43
The model will automatically check the database configuration array in ` app/config/database.php ` for a 'mongodb' item.
50
44
51
- ``` php
52
- 'mongodb' => array(
53
- 'host' => 'localhost',
54
- 'port' => 27017,
55
- 'database' => 'database',
56
- ),
57
- ```
45
+ 'mongodb' => array(
46
+ 'host' => 'localhost',
47
+ 'port' => 27017,
48
+ 'database' => 'database',
49
+ ),
58
50
59
51
You can also specify the connection name in the model:
60
52
61
- ``` php
62
- class MyModel extends Eloquent {
63
-
64
- protected $connection = 'mongodb2';
65
-
66
- }
67
- ```
53
+ class MyModel extends Eloquent {
54
+
55
+ protected $connection = 'mongodb2';
56
+
57
+ }
68
58
69
59
Examples
70
60
--------
71
61
72
62
** Retrieving All Models**
73
63
74
- ``` php
75
- $users = User::all();
76
- ```
64
+ $users = User::all();
77
65
78
66
** Retrieving A Record By Primary Key**
79
67
80
- ``` php
81
- $user = User::find('517c43667db388101e00000f');
82
- ```
68
+ $user = User::find('517c43667db388101e00000f');
83
69
84
70
** Wheres**
85
71
86
- ``` php
87
- $users = User::where('votes', '>', 100)->take(10)->get();
88
- ```
72
+ $users = User::where('votes', '>', 100)->take(10)->get();
89
73
90
74
** Or Statements**
91
75
92
- ``` php
93
- $users = User::where('votes', '>', 100)->orWhere('name', 'John')->get();
94
- ```
76
+ $users = User::where('votes', '>', 100)->orWhere('name', 'John')->get();
95
77
96
78
** Using Where In With An Array**
97
79
98
- ``` php
99
- $users = User::whereIn('age', array(16, 18, 20))->get();
100
- ```
80
+ $users = User::whereIn('age', array(16, 18, 20))->get();
101
81
102
82
When using ` whereNotIn ` objects will be returned if the field is non existant. Combine with ` whereNotNull('age') ` to leave out those documents.
103
83
104
84
** Using Where Between**
105
85
106
- ``` php
107
- $users = User::whereBetween('votes', array(1, 100))->get();
108
- ```
86
+ $users = User::whereBetween('votes', array(1, 100))->get();
109
87
110
88
** Where null**
111
89
112
- ``` php
113
- $users = User::whereNull('updated_at')->get();
114
- ```
90
+ $users = User::whereNull('updated_at')->get();
115
91
116
92
** Order By**
117
93
118
- ``` php
119
- $users = User::orderBy('name', 'desc')->get();
120
- ```
94
+ $users = User::orderBy('name', 'desc')->get();
121
95
122
96
** Offset & Limit**
123
97
124
- ``` php
125
- $users = User::skip(10)->take(5)->get();
126
- ```
98
+ $users = User::skip(10)->take(5)->get();
127
99
128
100
** Distinct**
129
101
130
102
Distinct requires a field for which to return the distinct values.
131
103
132
- ``` php
133
- $users = User::distinct()->get(array('name'));
134
- // or
135
- $users = User::distinct('name')->get();
136
- ```
104
+ $users = User::distinct()->get(array('name'));
105
+ // or
106
+ $users = User::distinct('name')->get();
137
107
138
108
Distinct can be combined with ** where** :
139
109
140
- ``` php
141
- $users = User::where('active', true)->distinct('name')->get();
142
- ```
110
+ $users = User::where('active', true)->distinct('name')->get();
143
111
144
112
** Advanced Wheres**
145
113
146
- ``` php
147
- $users = User::where('name', '=', 'John')->orWhere(function($query)
114
+ $users = User::where('name', '=', 'John')->orWhere(function($query)
148
115
{
149
116
$query->where('votes', '>', 100)
150
117
->where('title', '<>', 'Admin');
151
118
})
152
119
->get();
153
- ```
154
120
155
121
** Group By**
156
122
157
123
Selected columns that are not grouped will be aggregated with the $last function.
158
124
159
- ``` php
160
- $users = Users::groupBy('title')->get(array('title', 'name'));
161
- ```
125
+ $users = Users::groupBy('title')->get(array('title', 'name'));
162
126
163
127
** Aggregation**
164
128
165
- ``` php
166
- $total = Order::count();
167
- $price = Order::max('price');
168
- $price = Order::min('price');
169
- $price = Order::avg('price');
170
- $total = Order::sum('price');
171
- ```
129
+ $total = Order::count();
130
+ $price = Order::max('price');
131
+ $price = Order::min('price');
132
+ $price = Order::avg('price');
133
+ $total = Order::sum('price');
172
134
173
135
Aggregations can be combined with ** where** :
174
136
175
- ``` php
176
- $sold = Orders::where('sold', true)->sum('price');
177
- ```
137
+ $sold = Orders::where('sold', true)->sum('price');
178
138
179
139
** Like**
180
140
181
- ``` php
182
- $user = Comment::where('body', 'like', '%spam%')->get();
183
- ```
141
+ $user = Comment::where('body', 'like', '%spam%')->get();
184
142
185
143
** Inserts, updates and deletes**
186
144
@@ -190,20 +148,14 @@ All basic insert, update, delete and select methods should be implemented.
190
148
191
149
Perform increments or decrements (default 1) on specified attributes:
192
150
193
- ``` php
194
- User::where('name', 'John Doe')->increment('age');
195
- User::where('name', 'Bart De Wever')->decrement('weight', 50);
196
- ```
151
+ User::where('name', 'John Doe')->increment('age');
152
+ User::where('name', 'Jaques')->decrement('weight', 50);
197
153
198
154
The number of updated objects is returned:
199
155
200
- ``` php
201
- $count = User->increment('age');
202
- ```
156
+ $count = User->increment('age');
203
157
204
158
You may also specify additional columns to update:
205
159
206
- ``` php
207
- User::where('age', '29')->increment('age', 1, array('group' => 'thirty something'));
208
- User::where('bmi', 30)->decrement('bmi', 1, array('category' => 'overweight'));
209
- ```
160
+ User::where('age', '29')->increment('age', 1, array('group' => 'thirty something'));
161
+ User::where('bmi', 30)->decrement('bmi', 1, array('category' => 'overweight'));
0 commit comments