Alexander Donauer
Alexander Donauer

Reputation: 33

Passing Data to view - Undefined variable

I'm new to the Laravel Framework and I somehow got an issue that bothers me for days now.

I just wanted to pass a simple variable to a blade view, but any version that I found in any documentation or question on this platform unfortunately didn't lead to a solution.

I created a route which looks like this:

Route::get('test', 'PageController@index');

The Code in the PageController looks like this:

public function index(){
   $datatopass='datahere';
   return view('admin.test', compact('datatopass'));
}

And then I got the View that looks like this:

@section('content')
{{$datatopass}}
@stop

The problem is that I get 2 Exeptions:


#ErrorException in d9e848d01f99ac2368ead804bd322152 line 3:
Undefined variable: datatopass (View: /home/vagrant/test/resources/views/admin/test.blade.php)

ErrorException in d9e848d01f99ac2368ead804bd322152 line 3:
Undefined variable: datatopass

I'm using homestead development environment which is set up in a VirtualBox.

Any ideas what I've done wrong?

I really tried every type of datapassing like

return view('test')->with('datatopass', $datatopass)` 
//or
return view('datatopass', $datatopass)

and so on.

I'll be greateful for your help.

Upvotes: 3

Views: 1846

Answers (1)

Nilesh
Nilesh

Reputation: 934

Try this::

return view('test')->with('datatopass', $datatopass);

And in View access it as

<?php echo $datatopass ?>

Upvotes: 2

Related Questions