如何使用Laravel Api Resources返回数组而不是对象

23c0lvtd  于 2022-12-01  发布在  其他
关注(0)|答案(1)|浏览(126)

我被要求返回这个json,我正在使用Laravel中的api资源:

{
  "data": [
    {
      "type": "users",
      "id": "1",
      "attributes": {
        "name": "test name",
        "lastname": "test lastname"
        "projects": 2
      },
      "relationships": {
        "projects": {
          "data": [
            {
              "id": 1,
              "type": "projects"
            }
          ]
        }
      }
    }
  ]
}

一个用户有一个第一个项目,我这样做:
Models/User.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class User extends Model
{
    use HasFactory;

    protected $guarded = ['id'];      

    public function firstProject()
    {
        return $this->hasOne(Project::class)->oldest();
    }

    public function projects()
    {
       return $this->hasMany(Project::class);
    }

}

ProjectCollection.php

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class ProjectCollection extends ResourceCollection
{ 
    public function toArray($request)
    {         
        return [
            'data' => $this->collection
        ];
    }  
}

ProjectResource.php

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class ProjectResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            "type" => "projects",
            "id" => $this->id               
        ];
    }
}

UserCollection.php

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class UserCollection extends ResourceCollection
{
    public function toArray($request)
    {
        return [
            'data' => $this->collection
        ];
    }          
}

UserResource.php

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    public function toArray($request)
    {
        
        return [
            "type" => "users",
            "id" => $this->id,
            "attributes" => [
                "name" => $this->name,
                "lastname" => $this->lastname
                "projects" => $this->whenCounted('projects')
            ],
            "relationships" => [
                "projects" => new ProjectCollection($this->firstProject),
            ]            
        ];
    }
}

UsersController.php

$users = User::with('firstProject')->withCount('projects')->latest()->limit(10)->get();

    return new UserCollection($users);

如果我这样做:

"relationships" => [
   "projects" => new ProjectCollection($this->firstProject),
 ]

我得到这个错误:
调用未定义的方法App\Models\Project::mapInto()
但如果我把它改成这样:

"relationships" => [
   "projects" => $this->firstProject,
]

它不显示错误,但是我得到了这个json:

...
...
"relationships": {
   "projects": 
       {
          "id": 1,
          "type": "projects"
       }
}
...
...

项目未 Package 在数据属性y中未显示**[]**
我能做什么?谢谢。

bkhjykvo

bkhjykvo1#

我只需要将$this-〉firstProject Package 在数组中

"relationships" => [
   "projects" => new ProjectCollection([$this->firstProject]),
]

现在它将其 Package 为data属性中的数组,尽管我只有一个项

"relationships": {
        "projects": {
          "data": [
            {
              "id": 1,
              "type": "projects"
            }
          ]
        }
      }

这就是我想要的行为,现在一切都很好

相关问题