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

Solution 1:

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

Solution 2:

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)

Solution 3:

In Laravel 8.5.*, you can use the static method make on the collection to get the same result. It would be like UserResource::make($this->user)