Search code examples
laravellaravel-5

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


I have Post and User model with one to one relation and it works well:

//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:

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:

//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 works 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.

This is my User resource class:

<?php

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 I am wrong?


Solution

  • The issue is that you use UserResource::collection($this->user) and you have just one element come from database, not list of collection. It would be solved if you used new UserResource($this->user) like this:

    <?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' => new UserResource($this->user),
            'published' => $this->published,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    
    }
    }