Carol.Kar
Carol.Kar

Reputation: 5355

Passing $object to view

I am using Laravel Framework 5.5.25 and PHP 7.1.12.

I would like to pass a defined object to the view. I tried the following:

Route::get('/tasks', function getTask(TaskDataTable $dataTable) {
    return $dataTable->render('users.index');
});

However, I am getting the following error:

Symfony\Component\Debug\Exception\FatalThrowableError thrown with message "Parse error: syntax error, unexpected 'getUsers' (T_STRING), expecting '('"

Stacktrace:
#0 Symfony\Component\Debug\Exception\FatalThrowableError in /home/ubuntu/workspace/routes/web.php:18

Any suggestions why?

Upvotes: 1

Views: 84

Answers (1)

lagbox
lagbox

Reputation: 50491

This is not valid PHP:

Route::get('/tasks', function getTask(TaskDataTable $dataTable) {
    return $dataTable->render('users.index');
});

You are attempting to define a function named getTask inside another statement.

I would imagine you want an anonymous function that can be passed around as a Closure:

Route::get('/tasks', function (TaskDataTable $dataTable) {
    ...
});

"Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses."

PHP manual - anonymous functions

"Class used to represent anonymous functions. ... Anonymous functions, implemented in PHP 5.3, yield objects of this type."

PHP manual - class Closure

Upvotes: 1

Related Questions