Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-mongodb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
laravel-mongodb
Commits
ade145c0
Commit
ade145c0
authored
Jul 21, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update README.md
parent
3a561f0f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
98 deletions
+50
-98
README.md
README.md
+50
-98
No files found.
README.md
View file @
ade145c0
...
...
@@ -12,175 +12,133 @@ Installation
Add the package to your
`composer.json`
or install manually.
```
yaml
{
"
require"
:
{
"
jenssegers/mongodb"
:
"
*"
{
"require":
{
"jenssegers/mongodb": "*"
}
}
}
```
Run
`composer update`
to download and install the package.
Add the service provider in
`app/config/app.php`
:
```
php
'Jenssegers\Mongodb\MongodbServiceProvider'
,
```
'Jenssegers\Mongodb\MongodbServiceProvider',
Usage
-----
Tell your model to use the MongoDB model and a MongoDB collection (alias for table):
```
php
use
Jenssegers\Mongodb\Model
as
Eloquent
class
MyModel
extends
Eloquent
{
protected
$collection
=
'mycollection'
;
}
```
use Jenssegers
\M
ongodb
\M
odel as Eloquent
class MyModel extends Eloquent {
protected $collection = 'mycollection';
}
Configuration
-------------
The model will automatically check the database configuration array in
`app/config/database.php`
for a 'mongodb' item.
```
php
'mongodb'
=>
array
(
'host'
=>
'localhost'
,
'port'
=>
27017
,
'database'
=>
'database'
,
),
```
'mongodb' => array(
'host' => 'localhost',
'port' => 27017,
'database' => 'database',
),
You can also specify the connection name in the model:
```
php
class
MyModel
extends
Eloquent
{
protected
$connection
=
'mongodb2'
;
}
```
class MyModel extends Eloquent {
protected $connection = 'mongodb2';
}
Examples
--------
**Retrieving All Models**
```
php
$users
=
User
::
all
();
```
$users = User::all();
**Retrieving A Record By Primary Key**
```
php
$user
=
User
::
find
(
'517c43667db388101e00000f'
);
```
$user = User::find('517c43667db388101e00000f');
**Wheres**
```
php
$users
=
User
::
where
(
'votes'
,
'>'
,
100
)
->
take
(
10
)
->
get
();
```
$users = User::where('votes', '>', 100)->take(10)->get();
**Or Statements**
```
php
$users
=
User
::
where
(
'votes'
,
'>'
,
100
)
->
orWhere
(
'name'
,
'John'
)
->
get
();
```
$users = User::where('votes', '>', 100)->orWhere('name', 'John')->get();
**Using Where In With An Array**
```
php
$users
=
User
::
whereIn
(
'age'
,
array
(
16
,
18
,
20
))
->
get
();
```
$users = User::whereIn('age', array(16, 18, 20))->get();
When using
`whereNotIn`
objects will be returned if the field is non existant. Combine with
`whereNotNull('age')`
to leave out those documents.
**Using Where Between**
```
php
$users
=
User
::
whereBetween
(
'votes'
,
array
(
1
,
100
))
->
get
();
```
$users = User::whereBetween('votes', array(1, 100))->get();
**Where null**
```
php
$users
=
User
::
whereNull
(
'updated_at'
)
->
get
();
```
$users = User::whereNull('updated_at')->get();
**Order By**
```
php
$users
=
User
::
orderBy
(
'name'
,
'desc'
)
->
get
();
```
$users = User::orderBy('name', 'desc')->get();
**Offset & Limit**
```
php
$users
=
User
::
skip
(
10
)
->
take
(
5
)
->
get
();
```
$users = User::skip(10)->take(5)->get();
**Distinct**
Distinct requires a field for which to return the distinct values.
```
php
$users
=
User
::
distinct
()
->
get
(
array
(
'name'
));
// or
$users
=
User
::
distinct
(
'name'
)
->
get
();
```
$users = User::distinct()->get(array('name'));
// or
$users = User::distinct('name')->get();
Distinct can be combined with
**where**
:
```
php
$users
=
User
::
where
(
'active'
,
true
)
->
distinct
(
'name'
)
->
get
();
```
$users = User::where('active', true)->distinct('name')->get();
**Advanced Wheres**
```
php
$users
=
User
::
where
(
'name'
,
'='
,
'John'
)
->
orWhere
(
function
(
$query
)
$users = User::where('name', '=', 'John')->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
```
**Group By**
Selected columns that are not grouped will be aggregated with the $last function.
```
php
$users
=
Users
::
groupBy
(
'title'
)
->
get
(
array
(
'title'
,
'name'
));
```
$users = Users::groupBy('title')->get(array('title', 'name'));
**Aggregation**
```
php
$total
=
Order
::
count
();
$price
=
Order
::
max
(
'price'
);
$price
=
Order
::
min
(
'price'
);
$price
=
Order
::
avg
(
'price'
);
$total
=
Order
::
sum
(
'price'
);
```
$total = Order::count();
$price = Order::max('price');
$price = Order::min('price');
$price = Order::avg('price');
$total = Order::sum('price');
Aggregations can be combined with
**where**
:
```
php
$sold
=
Orders
::
where
(
'sold'
,
true
)
->
sum
(
'price'
);
```
$sold = Orders::where('sold', true)->sum('price');
**Like**
```
php
$user
=
Comment
::
where
(
'body'
,
'like'
,
'%spam%'
)
->
get
();
```
$user = Comment::where('body', 'like', '%spam%')->get();
**Inserts, updates and deletes**
...
...
@@ -190,20 +148,14 @@ All basic insert, update, delete and select methods should be implemented.
Perform increments or decrements (default 1) on specified attributes:
```
php
User
::
where
(
'name'
,
'John Doe'
)
->
increment
(
'age'
);
User
::
where
(
'name'
,
'Bart De Wever'
)
->
decrement
(
'weight'
,
50
);
```
User::where('name', 'John Doe')->increment('age');
User::where('name', 'Jaques')->decrement('weight', 50);
The number of updated objects is returned:
```
php
$count
=
User
->
increment
(
'age'
);
```
$count = User->increment('age');
You may also specify additional columns to update:
```
php
User
::
where
(
'age'
,
'29'
)
->
increment
(
'age'
,
1
,
array
(
'group'
=>
'thirty something'
));
User
::
where
(
'bmi'
,
30
)
->
decrement
(
'bmi'
,
1
,
array
(
'category'
=>
'overweight'
));
```
\ No newline at end of file
User::where('age', '29')->increment('age', 1, array('group' => 'thirty something'));
User::where('bmi', 30)->decrement('bmi', 1, array('category' => 'overweight'));
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment