laravel API 资源调用未定义的方法 Illuminate\Database\Query\Builder::mapInto()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47710805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:51:00  来源:igfitidea点击:

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

laravelapilaravel-5.5

提问by DaveIt

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

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

//User.php

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


// Post.php

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

now i create API resources:

现在我创建 API 资源:

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

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

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

//web.php: /resource/posts

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

this is my Posts resource class:

这是我的帖子资源类:

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

}
}

this is the error:

这是错误:

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

if i remove:

如果我删除:

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

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.

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

this is my User resource class:

这是我的用户资源类:

```

``

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

any ideas where am i wrong?

任何想法我错在哪里?

回答by Maraboc

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 :

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

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

回答by Amirex

this problem is that you use UserResource::collection($this->user) its mean you are many users but and you have just one element not a collection so you can replace it with new UserResource($this->user)

这个问题是你使用 UserResource::collection($this->user) 这意味着你有很多用户但是你只有一个元素不是集合所以你可以用新的 UserResource($this->user) 替换它