laravel android api,Laravel Route Api result

问题

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值