Passing data to view - variable Undefined

I am new to Laravel Framework and I somehow got a problem that has been bothering me for days.

I just wanted to pass a simple variable as a blade, but any version I found in any documentation or question on that platform unfortunately did not lead to a solution.

I created a route that looks like this:

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

      

The code PageController

looks like this:

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

      

And then I got a view that looks like this:

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

      

The problem is I am getting 2 exceptions:

#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 am using homestead development environment which is set up in VirtualBox.

Any ideas what I did wrong?

I have actually tried every datapassing type as

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

      

etc.

I will be grateful for your help.

+3


source to share


1 answer


Try the following:

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

      



And in the view View access to it like

<?php echo $datatopass ?>

      

+1


source







All Articles