Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Sep 19, 2018
1 parent 2b2b7b9 commit 0de8189
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions eloquent-resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,21 @@ 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:
After defining your resource collection, it may be returned from a route or controller:

use App\User;
use App\Http\Resources\UserCollection;

Route::get('/users', function () {
return new UserCollection(User::all());
});

#### Customizing The Underlying Resource Class

Typically, the `$this->collection` property of a resource collection is automatically populated with the result of mapping each item of the collection to its singular resource class. The singular resource class is assumed to be the collection's class name without the trailing `Collection` string.

For example, `UserCollection` will attempt to map the given user instances into the `User` resource. To customize this behavior, you may override the `$collects` property of your resource collection:

<?php

Expand All @@ -129,23 +140,14 @@ You can define `$this->collects` to override this default behaviour:

class UserCollection extends ResourceCollection
{
public $collects = 'Member';
public function toArray($request)
{
// ...
}
/**
* The resource that this resource collects.
*
* @var string
*/
public $collects = 'App\Http\Resources\Member';
}

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

use App\User;
use App\Http\Resources\UserCollection;

Route::get('/users', function () {
return new UserCollection(User::all());
});

<a name="writing-resources"></a>
## Writing Resources

Expand Down

0 comments on commit 0de8189

Please sign in to comment.