-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix attribute vs relation priorty #2577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 5.x
Are you sure you want to change the base?
Changes from 24 commits
2e46a6d
29935f2
a091681
07de6e5
9430d08
77dc9be
80527f5
e4fbb8d
87b6453
0a73a11
5385aa4
19db89b
21fac7d
d0666b6
dbfa900
61842f5
c8a6219
f18bf78
c0e1ee3
7a1fb98
61a0505
09c1a9d
815f37e
21a46e1
cc1631e
ed45cf7
e3330ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -856,19 +856,23 @@ class User extends Model | |
} | ||
} | ||
``` | ||
**Warning:** naming the foreign key same as the relation name will prevent the relation for being called on dynamic property, i.e. in the example above if you replaced `group_ids` with `groups` calling `$user->groups` will return the column instead of the relation. | ||
|
||
### EmbedsMany Relationship | ||
|
||
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. | ||
|
||
**REMEMBER**: These relations return Eloquent collections, they don't return query builder objects! | ||
|
||
**Breaking changes** starting from v4.0 you need to define the return type of EmbedsOne and EmbedsMany relation for it to work | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Very smart. Can you please provide a link to where it's done like this in laravel. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
```php | ||
use Jenssegers\Mongodb\Eloquent\Model; | ||
use Jenssegers\Mongodb\Relations\EmbedsMany; | ||
|
||
class User extends Model | ||
{ | ||
public function books() | ||
public function books(): EmbedsMany | ||
{ | ||
return $this->embedsMany(Book::class); | ||
} | ||
|
@@ -937,10 +941,11 @@ Like other relations, embedsMany assumes the local key of the relationship based | |
|
||
```php | ||
use Jenssegers\Mongodb\Eloquent\Model; | ||
use Jenssegers\Mongodb\Relations\EmbedsMany; | ||
|
||
class User extends Model | ||
{ | ||
public function books() | ||
public function books(): EmbedsMany | ||
{ | ||
return $this->embedsMany(Book::class, 'local_key'); | ||
} | ||
|
@@ -953,12 +958,15 @@ Embedded relations will return a Collection of embedded items instead of a query | |
|
||
The embedsOne relation is similar to the embedsMany relation, but only embeds a single model. | ||
|
||
**Breaking changes** starting from v4.0 you need to define the return type of EmbedsOne and EmbedsMany relation for it to work | ||
|
||
```php | ||
use Jenssegers\Mongodb\Eloquent\Model; | ||
use Jenssegers\Mongodb\Relations\EmbedsOne; | ||
|
||
class Book extends Model | ||
{ | ||
public function author() | ||
public function author(): EmbedsOne | ||
{ | ||
return $this->embedsOne(Author::class); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.