Laravel Route Api结果 [英] Laravel Route Api result

查看:125
本文介绍了Laravel Route Api结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想通过此路由API返回有关我的Json结果的一些属性:

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;    

});

例如,我只想在答复中得到名称和城市,但我也得到带有2个其他属性的完整对象.

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; 

});

推荐答案

自定义响应和嵌套资源的最简洁方法是使用 API资源.从文档中:

The cleanest way to customize responses and nested resources is using API Resources. From the docs:

构建API时,您可能需要一个位于 在您的口才模型和实际的JSON响应之间 返回给您应用程序的用户. Laravel的资源类允许 您可以轻松而富有表现力地转换您的模型和模型 收集到JSON中.

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.

生成API资源

php artisan make:resource ClubResource

然后,根据需要修改资源类:

App \ Http \ Resources \ ClubResource.php

<?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,
        ];
    }
}

使用API​​资源

然后在您的控制器中执行以下操作:

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

}

嵌套子资源(来自关系)

如您所见,您可以为任何模型创建API资源,因此当然可以创建子模型的资源类,然后将其用于格式化此类集合.您甚至可以在某些父资源类中使用它,例如 docs 说:

App \ Http \ Resources \ ClubResource.php

<?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),

        ];
    }
}

这篇关于Laravel Route Api结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆