为同一个 Laravel Model 创建多个 API Resources

Laravel

大家都已经知道 Laravel 的 API 资源是如何运行的。资源或者资源集合绑定到单个的模型(资源类和模型类使用相同的名字)。 通过模型的名称,资源可以与模型相互对应。有时候,为了实现不同的开发需求,相同的模型,我们也许需要返回不同的集合数据。例如,这么说吧,对于Employee 表,我们有时只需要 id  和 name ,但有时需要 idnamephoneaddress等。这种情况,我们需要创建不同的资源类来对应同一个模型。但是我们说,一个模型绑定到一个资源。不能创建多个同名的资源。通常 Laravel 不会允许这么做。

所以我们该怎么办呢!!!

好吧,我找到了两种方法来实现这个目标。

第一种方法 是通过在不同的文件夹中创建具有相同模型名的资源类

php artisan make:resource Employee
php artisan make:resource Another/Employee

第一个命令将创建资源文件 Employee.php在 app\Http\Resources  目录中。
第二个会创建 Employee.php 在 app\Http\Resources\Another  目录中。 但这是一个非常糟糕的方法不应采用这种方法。

第二种方法 是根据需要创建的资源。每个资源都继承了 JsonResource。 现在, 无论我们要与资源绑定哪个模型,我们都可以使用构造函数绑定它。

class EmployeeInfo extends JsonResource
{
    function __construct(Employee $model)
    {
        parent::__construct($model);
    }

    // ...
}

在这里,我将用下面的例子来解释这个问题。

例子

现在,我们有两个模型。 Employee 和 Gender。这两个模型之间的关系是,

<?php

namespace App\Models\Employee;

...

class Employee extends Model
{
    ...
    function gender(){
        return $this->belongsTo('App\Models\Gender', 'gender_id', 'id');
    }
}

genders 表的迁移文件


class CreateGendersTable extends Migration
{
    public function up()
    {
        Schema::create('genders', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });
    }

    ...
}

创建员工表的迁移文件

class CreateEmployeesTable extends Migration
{
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->unsignedBigInteger('gender_id');
            $table->timestamps();
        });
    }
   ...
}

现在,我将创建两个资源 。

第一个资源 ,

这个资源绑定员工模型通常称为资源模型绑定。它将返回员工的 id 和 name 。

class Employee extends JsonResource
{

    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
        ];
    }
}

第二个资源,

它将返回员工的 id 、name 和性别。这里,Employee 模型对象通过构造函数传递。

class EmployeeGenderInfo extends JsonResource
{
    function __construct(Employee $model)
    {
        parent::__construct($model);
    }
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'gender_name' => $this->gender->name,       
        ];
    }
}

现在 $this 关键字引用 Employee 模型。所以现在这个资源也可以使用 Employee 模型数据返回。

还有一件事

我们只需要在这个 resource 类中通过构造函数定义一次这个 model 模型。如果我们具有相同资源的资源集合,则无需再次定义该 model 模型。 我们可以像其他资源集合一样正常地编写资源集合。 就像在给定的示例中一样,对于第二个资源的资源收集,只需编写

class EmployeeGenderInfo extends ResourceCollection
{

    public function toArray($request)
    {

        return [
            'data' => $this->collection,
            'meta' => ['api-version' => '1.0']
        ];
    }
}

这是我的第一个博客。 请原谅我,如果有任何问题,您也可以分享您的意见。 这样,我可以纠正我的错误。

谢谢。

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://medium.com/@nazem.piash10/larave...

译文地址:https://learnku.com/laravel/t/36027

本文为协同翻译文章,如您发现瑕疵请点击「改进」按钮提交优化建议
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!