Skip to content

Commit 6b7805d

Browse files
committed
wip
1 parent f3dde4b commit 6b7805d

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

docs/docs/3.0/repository-pattern/field.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,27 @@ Field::new('user_id')->hidden()->append(function(RestifyRequest $request, $model
159159
return auth()->user()->id;
160160
});
161161
```
162+
163+
## Field label
164+
165+
- Field label, so you can replace a field attribute:
166+
```
167+
Field::new('created_at')->label('sent_at')
168+
```
169+
- Field can be setup as hidden:
170+
```
171+
Field::new('token')->hidden(); // this will not be visible
172+
```
173+
- Field can have append value, to append information like auth user, or any other relationships:
174+
```
175+
Field::new('user_id')->hidden()->append(auth()->user()->id); // this will not be visible, but will be stored
176+
```
177+
178+
- Related repositories no longer requires a `viaRelationship` query param, as it will get the default one from the main repository:
179+
Before:
180+
181+
` axios.get('/restify/users?viaRelationship=users&viaRepositoryId=1&viaRepository=companies')`
182+
183+
After:
184+
185+
` axios.get('/restify/users?viaRepositoryId=1&viaRepository=companies')`

docs/docs/3.0/repository-pattern/repository-pattern.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,71 @@ public static function routes(Router $router, $attributes = ['namespace' => 'App
513513
:::warning Clean routes
514514
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.
515515
:::
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)
551+
{
552+
$roles = collect($request->get('roles'))->map(fn($role) => Role::findByName($role, 'web'));
553+
554+
if ($id = $request->get('company_id')) {
555+
$user->assignCompanyRoles(
556+
Company::find($id),
557+
$roles
558+
);
559+
}
560+
561+
return $this->response()->created();
562+
}
563+
```
564+
The javascript request will remain the same:
565+
566+
```javascript
567+
axios.post('restify-api/users/1/attach/roles', [
568+
'roles': [1, 2]
569+
])
570+
```
571+
572+
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) {
580+
//
581+
},
582+
}
583+
```

0 commit comments

Comments
 (0)