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
Copy file name to clipboardExpand all lines: docs/docs/3.0/repository-pattern/repository-pattern.md
+68Lines changed: 68 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -513,3 +513,71 @@ public static function routes(Router $router, $attributes = ['namespace' => 'App
513
513
:::warning Clean routes
514
514
If `$wrap` is false, your routes will have any Route group `$attributes`, that means no prefix, middleware, or namespace will be applied out of the box, even you defined that as a default argument in the `routes` method. So you should take care of that.
515
515
:::
516
+
517
+
## Attach related
518
+
519
+
- Attach related models to a model (check tests)
520
+
- Attach multiple related model to a model
521
+
- Attach extra information to the pivot
522
+
523
+
Example of how to attach users posts to users with `is_owner` extra pivot:
524
+
```javascript
525
+
axios.post('restify-api/users/1/attach/posts', [
526
+
'posts': [1, 2],
527
+
'is_owner': true
528
+
])
529
+
```
530
+
531
+
## Detach related
532
+
533
+
- Detach repository
534
+
- Detach multiple repositories
535
+
536
+
Example of how to remove posts from user:
537
+
538
+
```javascript
539
+
axios.post('restify-api/users/1/detach/posts', [
540
+
'users': [1, 2]
541
+
]);
542
+
```
543
+
544
+
## Write your own attach
545
+
546
+
If you want to implement attach method for such relationship on your own, Laravel Restify provides you an easy way of doing that. Let's say you have to attach roles to user:
547
+
548
+
```php
549
+
// app/Restify/UserRepository.php
550
+
public function attachRoles(RestifyRequest $request, UserRepository $repository, User $user)
Based on your related resource, `roles`, Laravel Restify will automatically detect the `attachRoles` method.
573
+
574
+
If you don't like this kind of `magic` stuff, you can override the `getAttachers` method, and return an associative array, where the key is the name of the related resource, and the value should be a closure which handle the action:
575
+
576
+
```php
577
+
public static function getAttachers(): array
578
+
{
579
+
'roles' => function(RestifyRequest $request, UserRepository $repository, User $user) {
0 commit comments