Skip to content

Commit

Permalink
docs added for InteractsWithRelations;
Browse files Browse the repository at this point in the history
  • Loading branch information
hans-thomas committed Jun 26, 2023
1 parent dcab0b4 commit 2dd7e0f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
58 changes: 55 additions & 3 deletions docs/content/docs/Basics/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class RelatedExamplesCollectionQuery extends CollectionQuery {
}
}
```

{{< tip >}}
It's recommended that suffix the class with `CollectionQuery` to avoid any conflict and mistake.
{{< /tip >}}
Expand Down Expand Up @@ -320,26 +321,27 @@ domain/api/namespace/name?includes=example
To eager load a relationship, you must pass the registered include using `includes` key.
{{< /tip >}}

#### Nested eager loads
### Nested eager loads

Valravn allows you to use nested eager loads. for that you just need to pass the
nested includes after the first one and split them using a `.` character.

```
domain/api/namespace/name?includes=example.owner
```

{{< tip >}}
We consider the ExampleResource class registered `owner` include. otherwise this will not work.
{{< /tip >}}

#### Actions
### Actions

There are some default actions that you can use on your api calls. you are free
to use an action for two relationship or different actions for any includes.
actions must split using `:` character.

{{< tip >}}
columns you pass as parameter to actions, must be in filterable list of related model.
Columns you pass as parameter to actions, must be in filterable list of related model.
{{< /tip >}}

{{< column "methods-container" >}}
Expand Down Expand Up @@ -400,3 +402,53 @@ domain/api/blog/categories?includes=posts:limit(5|2)

For example, the above request will skip the first 5 posts and loads the second
5 posts of each category.

### Interact with relations

You can interact with relations and manipulate the relationships' data.

{{< column "methods-container" >}}

{{< column "method" >}}
[resolveRelationsUsing](#resolverelationsusing)
{{< /column >}}

{{< column "method" >}}
[skipRelationsForModel](#skiprelationsformodel)
{{< /column >}}

{{< /column >}}

{{< tip >}}
These methods just effect on current resource instance and other instances that created automatically using current
resource.
{{< /tip >}}

##### resolveRelationsUsing

Accepts relations and their custom resource class to be response to the client.

```php
PostResource::make( $this->post )
->resolveRelationsUsing(
[
'comments' => CommentCustomCollection::class
]
)
```

##### skipRelationsForModel

Accept models and their related relationships that should be skipped and not present in the output.

```php
PostResource::make( $this->post )
->skipRelationsForModel(
[
Post::class => 'comments',
Comment::class => [ 'post', 'user' ]
]
)
```


16 changes: 14 additions & 2 deletions tests/Core/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace Hans\Valravn\Tests\Core\Models;

use Hans\Valravn\Tests\Core\Factories\PostFactory;
use Hans\Valravn\Models\Contracts\Filterable;
use Hans\Valravn\Models\Contracts\Loadable;
use Hans\Valravn\Models\ValravnModel;
use Hans\Valravn\Tests\Core\Factories\PostFactory;
use Hans\Valravn\Tests\Core\Resources\Comment\CommentCollection;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Post extends ValravnModel implements Filterable {
class Post extends ValravnModel implements Filterable, Loadable {
use HasFactory;

protected $fillable = [
Expand Down Expand Up @@ -46,4 +48,14 @@ public function getFilterableAttributes(): array {
];
}

/**
* List of relationships that can be loaded
*
* @return array
*/
public function getLoadableRelations(): array {
return [
'comments' => CommentCollection::class,
];
}
}

1 comment on commit 2dd7e0f

@vercel
Copy link

@vercel vercel bot commented on 2dd7e0f Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

valravn – ./

valravn.vercel.app
valravn-hans-thomas.vercel.app
valravn-git-master-hans-thomas.vercel.app

Please sign in to comment.