1

I have an ArticleCommentsController with an index method

class ArticleCommentsController extends BaseController
{
    public function index($id)
    {

        $comments = DB::table('comments')
            ->leftJoin('users', 'users.id', '=', 'comments.user_id')
            ->where('comments.article_id', '=', $id)
            ->get();

         return $this->response->item($comments, new CommentTransformer);
    }
}

This is the transformer class

namespace App\Transformers;

use League\Fractal\TransformerAbstract;

class CommentTransformer extends TransformerAbstract{
    public function transform($comment)
    {
        return $comment; //simplified
    }
}

The response is the following error:

get_class() expects parameter 1 to be object, array given.

Obviously, i need to send an instance of the comment object when calling Fractal\transform but i don't know how to do that since laravel's raw queries only return an array or an instance of the QueryBuilder class.

lagbox
  • 48,571
  • 8
  • 72
  • 83
adetoola
  • 716
  • 1
  • 9
  • 19
  • Try removing the `->get()` from your query – smartrahat Jan 23 '16 at 13:05
  • @smartrahat. i did that but then it returned an instance of the QueryBuilder Object and returned this error: `Argument 1 passed to Dingo\Api\Http\Response\Factory::collection() must be an instance of Illuminate\Support\Collection, instance of Illuminate\Database\Query\Builder given, called in C:\xampp\htdocs\escape\app\Http\Controllers\ArticlesCommentsController.php on line 41 and defined` – adetoola Jan 23 '16 at 16:01

3 Answers3

2

Sadly, the item method on the response object seems to require and object and not an array. Using the array method will work, but won't use any transformer you pass.

So, I think you might get away using ArrayObject, as follow:

return $this->response->item(new ArrayObject($comments), new CommentTransformer);

Remember to put a use ArrayObject; at the top of the file.

Jonathan Urzúa
  • 156
  • 2
  • 4
1

this was very time long ago but I write the answers for this guy or others or me in the future if I lost my memory hahaha

class ArticleCommentsController extends BaseController
{
    public function index($id)
    {

        $comments = DB::table('comments')
            ->leftJoin('users', 'users.id', '=', 'comments.user_id')
            ->where('comments.article_id', '=', $id)
            ->get();

         return $this->response->collection(Collection::make($comments), new CommentTransformer);

    }
}

of course you need add this to the controller ArticleCommentsController

// Dingo
use Dingo\Api\Routing\Helpers;

//Convert query to collective
use Illuminate\Support\Collection;

//Transformers for API
use App\Transformers\CommentTransformer;

and this inside your controller before your functions

//Use for Dingo Helpers
use Helpers;

All together:

<?php

namespace App\Http\Controllers;
use Response;
use App\User;
use App\Http\Requests;
use Illuminate\Http\Request;

// Dingo
use Dingo\Api\Routing\Helpers;

//Convert query from LMS lbrary to collective
use Illuminate\Support\Collection;

//Transformers for API
use App\Transformers\CommentTransformer;

class ArticleCommentsController extends BaseController
{

    //Use for Dingo Helpers
    use Helpers;

    public function index($id)
    {

        $comments = DB::table('comments')
            ->leftJoin('users', 'users.id', '=', 'comments.user_id')
            ->where('comments.article_id', '=', $id)
            ->get();

         return $this->response->collection(Collection::make($comments), new CommentTransformer);

    }
}

Regards!, I hope this help to others in the future :D

Diego Cortés
  • 427
  • 2
  • 5
  • 11
1

Do the following steps ,it works out:

1.change

return $this->response->item($comments, new CommentTransformer);

to

return $this->response->collection(Collection::make($comments), new CommentTransformer);

2.Transfomer class

namespace App\Transformers;
use League\Fractal\TransformerAbstract;

class CommentTransformer extends TransformerAbstract{
     public function transform($comment)
     {
         return [
            'id' => $comment->id,
            ...
         ];
     }
}
yangwendaxia
  • 179
  • 2
  • 7