标签: laravel-resource

Laravel变压器vs资源

我对Laravel雄辩的资源和转换器之间的区别感到困惑。什么时候应该使用资源,什么时候应该使用变压器。从laravel文档中篇文章对此进行了一些研究,但其目的相同。

可能是我对此有错误的观念。任何人的知识共享将不胜感激。

php laravel laravel-resource

8
推荐指数
1
解决办法
3396
查看次数

是否可以通过 [Web] Guard 使用 Laravel 资源?

我正在使用 Ajax 请求来获取数据,它返回一个 Json 响应数据,该数据与我在创建 API 时将返回的数据相同。

问题是:我可以创建 Laravel 资源类并使用 (web), (API) Guards 返回该数据吗?

auth.php 文件

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],
Run Code Online (Sandbox Code Playgroud)

php laravel laravel-resource

5
推荐指数
0
解决办法
123
查看次数

Laravel-如何将API资源递归转换为数组?

我正在使用Laravel API资源,并希望将实例的所有部分都转换为数组。

在我的PreorderResource.php

/**
 * Transform the resource into an array.
 *
 * @param  \Illuminate\Http\Request
 * @return array
 */
public function toArray($request)
{
    return [
        'id' => $this->id,
        'exception' => $this->exception,
        'failed_at' => $this->failed_at,
        'driver' => new DriverResource(
            $this->whenLoaded('driver')
        )
    ];
}
Run Code Online (Sandbox Code Playgroud)

然后解决:

$resolved = (new PreorderResource(
  $preorder->load('driver')
))->resolve();
Run Code Online (Sandbox Code Playgroud)

乍一看,该方法可以解决问题,但问题是它无法递归工作。我的资源解析如下:

array:3 [
  "id" => 8
  "exception" => null
  "failed_at" => null
  "driver" => Modules\User\Transformers\DriverResource {#1359}
]
Run Code Online (Sandbox Code Playgroud)

如何解析API资源以递归数组?

php laravel laravel-response laravel-5.6 laravel-resource

3
推荐指数
2
解决办法
1508
查看次数

使用 Laravel 一次调用多个 API 资源

我正在使用 Laravel 的 API 资源将资源转换为 API 调用的数组,并且它工作正常,我是否可以在一次调用中检索多个模型的数据?至于获取用户的 JSON 数据和 Pages JSON ?或者我需要一个单独的电话。

这是我迄今为止尝试过的

//Controller
public function index(Request $request)
{
    $users = User::all();
    $pages = Page::all();
    return new UserCollection($users);
}

//API Resource
public function toArray($request)
    {
        return [
            'name' => $this->name,
            'username' => $this->username,
            'bitcoin' => $this->bitcoin,
        ];
    }
Run Code Online (Sandbox Code Playgroud)

任何帮助都将受到高度赞赏

laravel laravel-5 laravel-resource

3
推荐指数
1
解决办法
3203
查看次数

Laravel 资源条件返回

我有简单的 Laravel 资源:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'unread' => $this->unread,
            'details' => new EmployeeAddressResource($this->employeeAddress),
        ];
    }
}
Run Code Online (Sandbox Code Playgroud)

这工作正常,现在我想让细节有条件:

       'details' => $this
        ->when((auth()->user()->role == 'company'), function () {
               return new EmployeeAddressResource($this->employeeAddress);
                }),
Run Code Online (Sandbox Code Playgroud)

它也可以正常工作,但是如何添加其他条件以返回其他资源?例如,如果角色是user我想获取资源:CompanyAddressResource

我试过这个:

       'details' => $this
        ->when((auth()->user()->role == 'company'), function () {
                    return new EmployeeAddressResource($this->employeeAddress); …
Run Code Online (Sandbox Code Playgroud)

laravel laravel-resource

2
推荐指数
1
解决办法
3130
查看次数

如何测试 Laravel 资源

我正在测试一个带有分页的 API 端点返回的 Laravel 资源

 public function test_showing_all_plans(): void
 {
    $plans = Plan::where('is_active', true)
        ->paginate(10);

    $resource = PlansResource::collection($plans);

    $this->withHeaders([
        'Accept' => 'application/json',
        'Content-Type' => 'application/json',
        'AppKey' => 5,
    ])
        ->json('GET', '/api/customer/plans')
        ->assertStatus(200)
        ->assertExactJson([
            $resource->response()->getData(true),
        ]);
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是返回的结果不一样,因为端点的路径不等于资源的返回。

这是从端点返回的结果:

"links": {
        "first": "http://www.site.local/api/customer/plans?page=1",
        "last": "http://www.site.local/api/customer/plans?page=3",
        "next": "http://www.site.local/api/customer/plans?page=2",
        "prev": null
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 3,
        "path": "http://www.site.local/api/customer/plans",
        "per_page": 10,
        "to": 10,
        "total": 24
    }
Run Code Online (Sandbox Code Playgroud)

这是从资源“ $resource->response()->getData(true)”返回的代码

 "links": {
            "first": "http://www.site.local?page=1",
            "last": "http://www.site.local?page=3",
            "next": "http://www.site.local?page=2",
            "prev": null
        }, …
Run Code Online (Sandbox Code Playgroud)

api tdd unit-testing laravel laravel-resource

2
推荐指数
1
解决办法
1961
查看次数

Laravel 5.6: Customise a paginated resource collection meta and links attributes

How can I customize Laravel ResourceCollection meta and links information.

Links should include only prev,next and self instead of first,last,prev,next that is by default.

Meta should include pagination iformation like: current_page, total_items, items_per_page, total_pages instead of current_page, from, last_page, path, per_page, to, total.

This is how meta and links information looks now in JSON response:

"meta": {
    "currentPage": 2,
    "current_page": 1,
    "from": 1,
    "last_page": 3,
    "path": "http://localhost:8000/api",
    "per_page": 5,
    "to": 5,
    "total": 14
},
"links": {
    "self": "http://localhost:8000/api",
    "first": "http://localhost:8000/api?page=1", …
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent laravel-5.6 laravel-resource

1
推荐指数
1
解决办法
4363
查看次数