问题
I would like to return only a few attributs on my Json result from this route API :
Route::get('clubs', function () {
$clubs = App\Clubs::all();
return $clubs;
});
For exemple i would like to get only the name and the city in my response but i get also the full object with 2 more attributs.
What i'm doing wrong ? thanks a lot in advance
Route::get('clubs', function () {
$clubs = App\Clubs::all();
foreach($clubs as $club){
$club['name'] = $club->name;
$club['city'] = $club->city;
}
return $clubs;
});
回答1:
The cleanest way to customize responses and nested resources is using API Resources. From the docs:
When building an API, you may need a transformation layer that sits
between your Eloquent models and the JSON responses that are actually
returned to your application's users. Laravel's resource classes allow
you to expressively and easily transform your models and model
collections into JSON.
Generating API Resources
php artisan make:resource ClubResource
Then, modify the resource class to your needs:
App\Http\Resources\ClubResource.php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class ClubResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'name' => $this->name,
'city' => $this->city,
];
}
}
Using API Resources
Then in your controller you do this:
App\Http\ClubController.php
use App\Http\Resources\ClubResource;
use App\Clubs;
class ClubController extends Controller
{
// some of your code
function index() {
$clubs = Clubs::all(); // I strongly recommend you to paginate results.
// This way to return a collection
return ClubResource::collection($clubs);
});
// for a route like: /clubs/{clubId}
function show(Request $request) {
$clubs = Clubs::find($request->clubId);
// This way to return a single resource
return new ClubResource($clubs);
});
// the rest of your code
}
Nesting child resources (from relationships)
As you can see, you can create API Resources for any model, so of course you can create a resource class of the child model and then can be used to format this kind of collection. You can even use it inside some parent resource class like this as the docs says:
App\Http\Resources\ClubResource.php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class ClubResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'name' => $this->name,
'city' => $this->city,
// I'm asuming here that a Club has many members
// You can do this to load the childs only when you specify it:
'members' => MemberResource::collection($this->whenLoaded('members')),
// Or like this to always return the child resources
'members' => MemberResource::collection($this->members),
];
}
}
回答2:
Route::get('clubs', function () {
return App\Clubs::select('name', 'city')->get();
});
回答3:
You can do
$clubs = App\Clubs::select('name','city')->get();
来源:https://stackoverflow.com/questions/48884562/laravel-route-api-result