Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transformer for Responding With A Collection Of Items #442

Closed
donnigunawan opened this issue Jun 1, 2015 · 4 comments
Closed

transformer for Responding With A Collection Of Items #442

donnigunawan opened this issue Jun 1, 2015 · 4 comments

Comments

@donnigunawan
Copy link

Hi, I have no idea on how to use transformer class for Responding With A Collection Of Items. I tried another response and it works but now the Responding With A Collection Of Items.
Here is my code, I select all group from database :

in UserController.php :
class UserController extends BaseController
{
public function show()
{
//use ControllerTrait;
$group = Sentry::findGroupById(1);

    return $this->response->collection($group, new GroupTransformer);
}

}

the transformer class is in app/transformer/GroupTransormer.php :

use League\Fractal\TransformerAbstract;

class GroupTransformer extends TransformerAbstract
{

/**
 * Turn this item object into a generic array
 *
 * @return array
 */
public function transform(User $user)
{
    return [
        'id'            => (int) $user->id,
        'name'          => $user->name,
        'permissions'         => $user->permissions,
    ];
}

}

Then when I access, I got this error :

ErrorException thrown with message "Argument 1 passed to Dingo\Api\Http\ResponseFactory::collection() must be an instance of Illuminate\Support\Collection, instance of Cartalyst\Sentry\Groups\Eloquent\Group given, called in /var/www/my-app/app/controllers/UserController.php on line 12 and defined"

Stacktrace:
#20 ErrorException in /var/www/my-app/vendor/dingo/api/src/Http/ResponseFactory.php:75
#19 Illuminate\Exception\Handler:handleError in /var/www/my-app/vendor/dingo/api/src/Http/ResponseFactory.php:75
#18 Dingo\Api\Http\ResponseFactory:collection in /var/www/my-app/app/controllers/UserController.php:12
#17 UserController:show in <#unknown>:0
#16 call_user_func_array in /var/www/my-app/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:231
#15 Illuminate\Routing\Controller:callAction in /var/www/my-app/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:93
#14 Illuminate\Routing\ControllerDispatcher:call in /var/www/my-app/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:62
#13 Illuminate\Routing\ControllerDispatcher:dispatch in /var/www/my-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php:967
#12 Illuminate\Routing\Router:Illuminate\Routing{closure} in <#unknown>:0
#11 call_user_func_array in /var/www/my-app/vendor/laravel/framework/src/Illuminate/Routing/Route.php:109
#10 Illuminate\Routing\Route:run in /var/www/my-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1033
#9 Illuminate\Routing\Router:dispatchToRoute in /var/www/my-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1001
#8 Illuminate\Routing\Router:dispatch in /var/www/my-app/vendor/dingo/api/src/Routing/Router.php:205
#7 Dingo\Api\Routing\Router:dispatch in /var/www/my-app/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:775
#6 Illuminate\Foundation\Application:dispatch in /var/www/my-app/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:745
#5 Illuminate\Foundation\Application:handle in /var/www/my-app/vendor/laravel/framework/src/Illuminate/Session/Middleware.php:72
#4 Illuminate\Session\Middleware:handle in /var/www/my-app/vendor/laravel/framework/src/Illuminate/Cookie/Queue.php:47
#3 Illuminate\Cookie\Queue:handle in /var/www/my-app/vendor/laravel/framework/src/Illuminate/Cookie/Guard.php:51
#2 Illuminate\Cookie\Guard:handle in /var/www/my-app/vendor/stack/builder/src/Stack/StackedHttpKernel.php:23
#1 Stack\StackedHttpKernel:handle in /var/www/my-app/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:641
#0 Illuminate\Foundation\Application:run in /var/www/my-app/public/index.php:49

@bweston92
Copy link
Contributor

You need to make a Illuminate collection out of the Sentry model.

@bweston92
Copy link
Contributor

After looking at Sentry, I've noticed you're passing a single model not a collection into the response builder.

@donnigunawan
Copy link
Author

thank you @bweston92 , I didn't notice that one

@vincentshiqi
Copy link

你们是如何解决此问题的啊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants