I can not send Blade variable in Laravel 5.1

I am trying to send a variable to a file blade view, but will skip this error:

Undefined variable: data (View: D: \ wamp \ www \ tienda \ resources \ views \ cliente.blade.php)

This is my route:

Route::resource('cliente','ClienteController'); 

This is my client controller:

 public function index(){ $data = Cliente::all(); return view('cliente',compact($data)); } 

And my blade:

  @foreach ($data as $user) <tr> <td>{{$user->nombre}}</td> </tr> @endforeach 

What am I doing wrong?

Also, if you try to do this, for example, this is the Cliente Controller:

  public function index(){ return view('cliente', ['name' => 'James']); } 

And the blade:

 {{$name}} 

That yes works ... Only variables and arrays do not work.

+5
source share
2 answers

Try this on your controller:

 public function index(){ $data = Cliente::all(); return view('cliente',compact('data')); } 

From the compact documentation : "Each parameter can be a string containing the name of the variable or array variable names. An array may contain other arrays of variable names inside it: compact () processes it recursively. "

+4
source

you can try this way

 public function index(){ $data['data'] = Cliente::all(); return view('cliente', $data); } 

Then you can catch it in the blade just like that.

  @foreach ($data as $user) <tr> <td>{{$user->nombre}}</td> </tr> @endforeach 
+1
source

All Articles