Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Allowing passing arrays to JsonResource #25213

Merged
merged 1 commit into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Illuminate/Http/Resources/Json/JsonResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ public function resolve($request = null)
*/
public function toArray($request)
{
return $this->resource->toArray();
return is_array($this->resource)
? $this->resource
: $this->resource->toArray();
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/Integration/Http/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Resources\MergeValue;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Tests\Integration\Http\Fixtures\Post;
use Illuminate\Tests\Integration\Http\Fixtures\Author;
use Illuminate\Http\Resources\ConditionallyLoadsAttributes;
Expand Down Expand Up @@ -704,4 +705,24 @@ public function work()
],
], $results);
}

public function test_the_resource_can_be_an_array()
{
Route::get('/', function () {
return new JsonResource([
'[email protected]' => 'John',
'[email protected]' => 'Hank',
]);
});

$this->withoutExceptionHandling()
->get('/', ['Accept' => 'application/json'])
->assertStatus(200)
->assertJson([
'data' => [
'[email protected]' => 'John',
'[email protected]' => 'Hank',
],
]);
}
}