Level 50
Looks like you forgot $ ... Change this to $this
'data' => this->transform($lessons)
4 likes
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi Awesome People
I was following Laracast's Api course
i'm getting following error
FatalErrorException in LessonsController.php line 19:
syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ']'
My Controller is
<?php
namespace App\Http\Controllers;
use App\lesson;
use Response;
use Illuminate\Http\Request;
class LessonsController extends Controller
{
//fetch all and pass a metadata 'data'
public function index() {
$lessons = Lesson::all();
return Response::json([
'data' => this->transform($lessons)
], 200);
}
//fetch by id
public function show($id) {
$lesson = Lesson::find($id);
if(! $lesson) {
return Response::json([
'error' => [
'message' => 'No Response Please Try Again'
]
], 404);
}
return Response::json([
'data' => $lesson->toArray()
], 200);
}
//transform the lessons data and return only requried fields
public function transform($lessons) {
return array_map(function($lesson) {
return [
'title' => $lesson['title'],
'body' => $lesson['body'],
'avtive' => $lesson['completed'],
];
}, $lessons->toArray());
}
}
Thank You
Looks like you forgot $ ... Change this to $this
'data' => this->transform($lessons)
Please or to participate in this conversation.