I'm using Laravel 5.3 with Laravel Fractal. I'm building a forum so when I want to receive all forums I do that like this:
$forums = Forum::with('messages', 'messages.user')
->latest()
->paginate(10);
return fractal()
->collection($forums)
->transformWith(new ForumTransformer())
->paginateWith(new IlluminatePaginatorAdapter($forums))
->respond();
Then in ForumTransformer I'm doing this:
<?php
namespace App\Jenssen\Forums;
use League\Fractal;
class ForumTransformer extends Fractal\TransformerAbstract
{
public function transform(Forum $forum)
{
return [
'id' => $forum->id,
'slug' => $forum->slug,
'name' => $forum->name,
'total_messages' => $forum->messages->count(),
'message_user' => $forum->messages->first()->user->name . ' ' . $forum->messages->first()->user->last_name,
'created_at' => $forum->created_at,
'updated_at' => $forum->updated_at
];
}
}
Is this a n+1
problem? Or does Laravel use the eager loaded relationship in the ForumTransformer class?