3

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.

8
  • Weird, the ->with() and compact ones should work. Have you tried dd($datatopass) just before you return your view? What does it say? Commented Jun 26, 2015 at 12:47
  • Are you sure that you don't load this view in different places other than PageController? Commented Jun 26, 2015 at 14:09
  • Yes the testpage ist just started by the PageController. And about dd(): no output here. Did I call it right.PageController public function index(){ $datatopass='datahere'; dd($datatopass); } Commented Jun 26, 2015 at 14:29
  • Try to do php artisan view:clear to reset views cache Commented Jun 26, 2015 at 15:47
  • There is no such namespace as view defined for php artisan Commented Jun 26, 2015 at 16:06

1 Answer 1

2

Try this::

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

And in View access it as

<?php echo $datatopass ?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. But I already solved the problem. Something was wrong in my route.php so the data wasn't passed to the right view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.