Skip to content

Commit a7db110

Browse files
author
Mostafa Kamal
committed
relationship laravel
1 parent 2c4b30e commit a7db110

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

web-developer/laravel/tips/relationship.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
1+
---
2+
description: Laravel Relationships
3+
keywords: laravel, relation, relationship
4+
title: Laravel Relationships
5+
toc_max: 4
6+
---
17

8+
## relationship
29

3-
* [pivot](https://laraveldaily.com/pivot-tables-and-many-to-many-relationships/)
10+
### one to one
11+
12+
* hasOne('Model\Name','foreign_key','local_key')
13+
* belongsTo('Model\Name','foreign_key','local_key')
14+
15+
### one to many
16+
17+
* hasMany('Model\Name','foreign_key','local_key')
18+
* belongsTo('Model\Name','foreign_key','local_key')
19+
20+
### many to many
21+
22+
* belongsToMany('App\Role', 'role_user', 'user_id', 'role_id')
23+
* role_user table
24+
25+
~
26+
27+
* role->attach($id|array $ids)
28+
* role->detach($id|array $ids) / role->detach()
29+
* role->sync([])
30+
* role->toggle([])
31+
32+
### hasOneThrough
33+
34+
* hasOneThrough('Model\Name','Model\Name1','foreign_key1','foreign_key','local_key','local_key1')
35+
36+
### hasManyThrough
37+
38+
* hasManyThrough('Model\Name','Model\Name1','foreign_key1','foreign_key','local_key','local_key1')
439

540
## Eager Loading
641

@@ -21,3 +56,8 @@ foreach ($users as $user) {
2156
echo $user->message->title;
2257
}
2358
```
59+
60+
61+
## resource
62+
63+
* [pivot](https://laraveldaily.com/pivot-tables-and-many-to-many-relationships/)

0 commit comments

Comments
 (0)