Compoot
Compoot

Reputation: 2387

Laravel syntax error in trying to load simple view: unexpected '=>' (T_DOUBLE_ARROW), expecting ']'

Just starting out with Laravel. I am simply trying to pass a variable into a data array and load the view, but come up with the following syntax error:

syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ']'

I've tried re-arranging the way I formatted the data array, to no avail. What am I missing?

hello.blade.php file

<html>
<body>
<h1><?php echo $someData ?></h1>
</body>
</html>

web.php (routes)

Route::get('/', function () {
    return view('welcome');
});


Route::get('/hello', function () {
    $variable = 'Hello from inside a v';
    return view('hello',data[
    'someData' => $variable,
    ]);
});

The error is coming up as being with this line:

'someData' => $variable,

I have tried it with and without the comma after $variable - but that didn't work either. Thanks in advance.

Note: I know I could use different notation or blade, but at the moment, I just want to know why this doesn't work. There is another stackoverflow question with a similar title, but it wasn't able to address my specific issue.

Upvotes: 0

Views: 720

Answers (1)

Jonathan
Jonathan

Reputation: 11494

Remove the data you have in there. It is a syntax error.

Route::get('/hello', function () {
    $variable = 'Hello from inside a v';
    return view('hello', [
        'someData' => $variable,
    ]);
});

Please also see Blade syntax in .blade.php files: https://laravel.com/docs/master/blade#displaying-data

hello.blade.php should be more like:

<html>
<body>
    <h1>{{ $someData }}</h1>
</body>
</html>

Upvotes: 1

Related Questions