adetoola
adetoola

Reputation: 716

Use laravel raw queries with dingo/APi + Fractal/transformers with laravel 5.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.

Upvotes: 1

Views: 1679

Answers (3)

yangwendaxia
yangwendaxia

Reputation: 179

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

Upvotes: 1

Diego Cortés
Diego Cortés

Reputation: 425

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

Upvotes: 1

Jonathan Urz&#250;a
Jonathan Urz&#250;a

Reputation: 156

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.

Upvotes: 2

Related Questions