Can you post your transformer code?
Nov 7, 2014
4
Level 11
L5 creating API with pagination - Issue
I am trying to create an API with Laravel 5 and in order to refresh my memory for a good architecture I followed again the "Incremental APIs" series. Everything worked perfectly until I tried to paginate my results, this is the error I have (I duplicate my code for help using the same situation as Jeffrey did so you will find 'Lessons' and 'title'):
ErrorException in LessonTransformer.php line 12:
Illegal string offset 'title'
at HandleExceptions->handleError('2', 'Illegal string offset 'title'', '/.../app/Transformers/LessonTransformer.php', '12', array('lessons' => 'http://.../api/v1/lessons/?page=2')) in LessonTransformer.php line 12
In my controller:
public function index(LessonTransformer $lessonTransformer)
{
$limit = Input::get('limit', 10);
$lessons = Lesson::paginate($limit);
return $this->respond([
'lessons' => $lessonTransformer->transformCollection($lessons)
]);
}
Only difference you can find is that I index the array with 'lessons' and not with 'data'. Also, I repeat, if in my controller I use Lesson::all() it works. it doesn't work the ->all() method though so I simply used ->toArray().
Hope someone can help, I don't know if it is my mistake or not, or if the issue is in L5
Please or to participate in this conversation.