Skip to content

Commit

Permalink
Update eloquent-resources.md
Browse files Browse the repository at this point in the history
Updates documentation to include an example of the collection resource `collects` public variable.
  • Loading branch information
r3oath authored Sep 18, 2018
1 parent 8aa8dd8 commit c3a9583
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions eloquent-resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,26 @@ Once the resource collection class has been generated, you may easily define any
];
}
}

As used above, `$this->collection` is automatically populated with the results of mapping each item of the collection over its singular resource. The singular resource is assumed to be defined as start of the collection's class name, without the trailing string `Collection` – for example `UserCollection` will search for the singluar resource `User`.

You can define `$this->collects` to override this default behaviour:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class UserCollection extends ResourceCollection
{
public $collects = 'Member';
public function toArray($request)
{
// ...
}
}

After defining your resource collection, it may be returned from a route or controller:

Expand All @@ -126,8 +146,6 @@ After defining your resource collection, it may be returned from a route or cont
return new UserCollection(User::all());
});

> {note} Collections will automatically utilize any singular resources defined (a resource class matching the start of the collection class name, e.g.: `JokeCollection` will try to match `Joke`.) If you wish to define what singular resource should be used instead of this default behaviour you can define `$this->collects` from within your collection class.
<a name="writing-resources"></a>
## Writing Resources

Expand Down

0 comments on commit c3a9583

Please sign in to comment.