0

I want to passing data from controller to view

$participantView = view('section.participant', ['data' => $result['data']])->render();

the $result['data'] is array data(not empty)

I do foreach in view, and got:

undefined variable: data

I also do with compact and 'with', but also getting undefined variable

What is wrong in my code?

halfer
  • 19,824
  • 17
  • 99
  • 186

4 Answers4

0

You must return the view, Try this :

return view('section.participant', ['data' => $result['data']]);
Youcef Moulahoum
  • 319
  • 2
  • 11
0

try using

 $data = $result['data'];
 return view('section.participant', compact('data'));

It will do work.

Himanshu Raval
  • 790
  • 7
  • 19
0

I want to passing the data to view, then I will return $data. Which is:

$data=['data_array' => ['view' => $participantView ]];

in another function, the passing data was fine.

halfer
  • 19,824
  • 17
  • 99
  • 186
0

Try this:

$participantView = view('section.participant')->with([
    'data' => $result['data']
])->render();

and in view you will have $data variable available.

Calin Blaga
  • 1,375
  • 12
  • 19
  • It's still same, still getting undefined variable data. I don't understand what's wrong, because in other function that work – Chandra Putra Wijaya Jun 10 '16 at 01:32
  • still same. or there is a wrong way from my code? i load js function from view, the js function is calling ajax to the controller, and then in the controller passing data to view. from the controller, that will return the view to ajax. is it wrong? – Chandra Putra Wijaya Jun 10 '16 at 06:43
  • It seams that your problem is there. You are returning the view content, a string, rather then a view object. It's not going to work that way. – Calin Blaga Jun 10 '16 at 06:49
  • Maybe this will help http://stackoverflow.com/questions/28634712/how-can-i-return-a-view-from-an-ajax-call-in-laravel-5 – Calin Blaga Jun 10 '16 at 06:52
  • but, for some reason. in other function(which is using the same function like that) that work XD. – Chandra Putra Wijaya Jun 10 '16 at 06:59