You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -419,7 +417,7 @@ Aggregations can be also used on sub-documents:
419
417
$total = Order::max('suborder.price');
420
418
```
421
419
422
-
**NOTE**: This aggregation only works with single sub-documents (like `EmbedsOne`) not subdocument arrays (like `EmbedsMany`).
420
+
**NOTE**: This aggregation only works with single sub-documents not arrays.
423
421
424
422
**Incrementing/Decrementing the value of a column**
425
423
@@ -712,10 +710,6 @@ The only available relationships are:
712
710
- belongsTo
713
711
- belongsToMany
714
712
715
-
The MongoDB-specific relationships are:
716
-
- embedsOne
717
-
- embedsMany
718
-
719
713
Here is a small example:
720
714
721
715
```php
@@ -764,152 +758,6 @@ class User extends Model
764
758
}
765
759
```
766
760
767
-
### EmbedsMany Relationship
768
-
769
-
If you want to embed models, rather than referencing them, you can use the `embedsMany` relation. This relation is similar to the `hasMany` relation but embeds the models inside the parent object.
770
-
771
-
**REMEMBER**: These relations return Eloquent collections, they don't return query builder objects!
772
-
773
-
```php
774
-
use Jenssegers\Mongodb\Eloquent\Model;
775
-
776
-
class User extends Model
777
-
{
778
-
public function books()
779
-
{
780
-
return $this->embedsMany(Book::class);
781
-
}
782
-
}
783
-
```
784
-
785
-
You can access the embedded models through the dynamic property:
786
-
787
-
```php
788
-
$user = User::first();
789
-
790
-
foreach ($user->books as $book) {
791
-
//
792
-
}
793
-
```
794
-
795
-
The inverse relation is auto*magically* available. You don't need to define this reverse relation.
796
-
797
-
```php
798
-
$book = Book::first();
799
-
800
-
$user = $book->user;
801
-
```
802
-
803
-
Inserting and updating embedded models works similar to the `hasMany` relation:
804
-
805
-
```php
806
-
$book = $user->books()->save(
807
-
new Book(['title' => 'A Game of Thrones'])
808
-
);
809
-
810
-
// or
811
-
$book =
812
-
$user->books()
813
-
->create(['title' => 'A Game of Thrones']);
814
-
```
815
-
816
-
You can update embedded models using their `save` method (available since release 2.0.0):
817
-
818
-
```php
819
-
$book = $user->books()->first();
820
-
821
-
$book->title = 'A Game of Thrones';
822
-
$book->save();
823
-
```
824
-
825
-
You can remove an embedded model by using the `destroy` method on the relation, or the `delete` method on the model (available since release 2.0.0):
826
-
827
-
```php
828
-
$book->delete();
829
-
830
-
// Similar operation
831
-
$user->books()->destroy($book);
832
-
```
833
-
834
-
If you want to add or remove an embedded model, without touching the database, you can use the `associate` and `dissociate` methods.
835
-
836
-
To eventually write the changes to the database, save the parent object:
837
-
838
-
```php
839
-
$user->books()->associate($book);
840
-
$user->save();
841
-
```
842
-
843
-
Like other relations, embedsMany assumes the local key of the relationship based on the model name. You can override the default local key by passing a second argument to the embedsMany method:
Embedded relations will return a Collection of embedded items instead of a query builder. Check out the available operations here: https://laravel.com/docs/master/collections
858
-
859
-
860
-
### EmbedsOne Relationship
861
-
862
-
The embedsOne relation is similar to the embedsMany relation, but only embeds a single model.
863
-
864
-
```php
865
-
use Jenssegers\Mongodb\Eloquent\Model;
866
-
867
-
class Book extends Model
868
-
{
869
-
public function author()
870
-
{
871
-
return $this->embedsOne(Author::class);
872
-
}
873
-
}
874
-
```
875
-
876
-
You can access the embedded models through the dynamic property:
877
-
878
-
```php
879
-
$book = Book::first();
880
-
$author = $book->author;
881
-
```
882
-
883
-
Inserting and updating embedded models works similar to the `hasOne` relation:
884
-
885
-
```php
886
-
$author = $book->author()->save(
887
-
new Author(['name' => 'John Doe'])
888
-
);
889
-
890
-
// Similar
891
-
$author =
892
-
$book->author()
893
-
->create(['name' => 'John Doe']);
894
-
```
895
-
896
-
You can update the embedded model using the `save` method (available since release 2.0.0):
897
-
898
-
```php
899
-
$author = $book->author;
900
-
901
-
$author->name = 'Jane Doe';
902
-
$author->save();
903
-
```
904
-
905
-
You can replace the embedded model with a new model like this:
In this new major release which supports the new MongoDB PHP extension, we also moved the location of the Model class and replaced the MySQL model class with a trait.
949
+
#### Upgrading from version 3 to 4
1104
950
1105
-
Please change all `Jenssegers\Mongodb\Model` references to `Jenssegers\Mongodb\Eloquent\Model` either at the top of your model files or your registered alias.
951
+
This new major release contains breaking changes which is listed below:
1106
952
1107
-
```php
1108
-
use Jenssegers\Mongodb\Eloquent\Model;
953
+
- EmbedsOne and EmbedsMany relations has been removed completely. See explanation [here](https://github.com/jenssegers/laravel-mongodb/issues/1974#issuecomment-592859508)
1109
954
1110
-
class User extends Model
1111
-
{
1112
-
//
1113
-
}
1114
-
```
1115
-
1116
-
If you are using hybrid relations, your MySQL classes should now extend the original Eloquent model class `Illuminate\Database\Eloquent\Model` instead of the removed `Jenssegers\Eloquent\Model`.
1117
-
1118
-
Instead use the new `Jenssegers\Mongodb\Eloquent\HybridRelations` trait. This should make things more clear as there is only one single model class in this package.
1119
-
1120
-
```php
1121
-
use Jenssegers\Mongodb\Eloquent\HybridRelations;
1122
-
1123
-
class User extends Model
1124
-
{
1125
-
1126
-
use HybridRelations;
1127
-
1128
-
protected $connection = 'mysql';
1129
-
}
1130
-
```
1131
-
1132
-
Embedded relations now return an `Illuminate\Database\Eloquent\Collection` rather than a custom Collection class. If you were using one of the special methods that were available, convert them to Collection operations.
1133
-
1134
-
```php
1135
-
$books = $user->books()->sortBy('title')->get();
1136
-
```
955
+
For any other minor changes, please take a look at our [changelog](CHANGELOG.md)
0 commit comments