laravel API 资源调用未定义的方法 Illuminate\Database\Query\Builder::mapInto() [英] laravel APi resource Call to undefined method Illuminate\Database\Query\Builder::mapInto()

查看:38
本文介绍了laravel API 资源调用未定义的方法 Illuminate\Database\Query\Builder::mapInto()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对一关系的 Post 和 User 模型,效果很好:

i have Post and User model with one to one relation and it works good:

//User.php

public function post(){
    return $this->hasOne(Post::class);
}


// Post.php

public function user() {
    return $this->belongsTo(User::class);
}

现在我创建 API 资源:

now i create API resources:

php artisan make:resource Post
php artisan make:resource User

我需要通过 api 调用返回所有帖子,然后我设置我的路线:

I need to return all post with an api call then i set my route:

//web.php: /resource/posts

Route::get('/resource/posts', function () {
    return PostResource::collection(Post::all());
});

这是我的帖子资源类:

<?php

namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
use App\Http\Resources\User as UserResource;

class Posts extends Resource
{
/**
 * Transform the resource into an array.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function toArray($request)
{
      return [
        'id' => $this->id,
        'title' => $this->title,
        'slug' => $this->slug,
        'bodys' => $this->body,
        'users' => UserResource::collection($this->user),
        'published' => $this->published,
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ];

}
}

这是错误:

Call to undefined method Illuminate\Database\Query\Builder::mapInto()

如果我删除:

'users' => UserResource::collection($this->user),

它的工作,但我需要在我的 api json 中包含关系,我已经阅读并遵循了 https 上的文档://laravel.com/docs/5.5/collections.

it's work but i need to include relations in my api json, i have read and followed the doc at https://laravel.com/docs/5.5/collections.

这是我的用户资源类:

```

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\Resource;

class User extends Resource
{
/**
 * Transform the resource into an array.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function toArray($request)
{
   return [
       'user_id' => $this->user_id,
       'name' => $this->name,
       'lastname' => $this->lastname,
       'email' => $this->email
   ];
}
}

任何想法我错在哪里?

推荐答案

问题是你使用了 UserResource::collection($this->user) 而你只有一个元素而不是集合,这样你就可以像这样用 new UserResource($this->user) 替换它:

The problem is that you use UserResource::collection($this->user) and you have just one element not a collection so you can replace it with new UserResource($this->user) like this :

return [
    'id' => $this->id,
    'title' => $this->title,
    'slug' => $this->slug,
    'bodys' => $this->body,
    'users' => new UserResource($this->user),
    'published' => $this->published,
    'created_at' => $this->created_at,
    'updated_at' => $this->updated_at,
];

这篇关于laravel API 资源调用未定义的方法 Illuminate\Database\Query\Builder::mapInto()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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