-1

I'm trying to post data from controller to view, basically when page loads user sees the form and when it get submits, it returns the data. I'm not sure what I'm doing wrong. I have tried the following to returns the data

Return methods tried:

return view('welcome',['all_data'=>$all_data]);
return view('welcome')->with('all_data', $all_data);
return view('welcome')->with('data', json_decode($data, true));
return View::make('welcome', array('all_data'=>$all_data));

Controller:

public function getStatus(Request $request){

//SQLQuery which returns $all_data

$all_data = json_encode($data);
return view('welcome', compact('all_data'));
}

Route:

Route::get('/', function () {
    return view('welcome');
});

Route::post('/getstatus', 'GetApplicationStatusController@getStatus');

View:

@foreach ($all_data as $data)
<td id="appid">{{$data->appid}}</td>
<td id="firstname">{{$data->firstname}}</td>
<td id="middlename">{{$data->middlename}}</td>
<td id="lastname">{{$data->lastname}}</td>
<td id="action">{{$data->action}}</td>
@endforeach
tereško
  • 58,060
  • 25
  • 98
  • 150
JKLM
  • 1,470
  • 3
  • 28
  • 66

3 Answers3

1

Pass all_data without json_encode for foreach

public function getStatus(Request $request){

    //SQLQuery which returns $data
    $all_data = $data;
    return view('welcome', compact('all_data'));
}

In view:

@foreach ($all_data as $data)
   <td id="appid">{{$data['appid']}}</td>
   <td id="firstname">{{$data['firstname']}}</td>
   <td id="middlename">{{$data['middlename']}}</td>
   <td id="lastname">{{$data['lastname']}}</td>
   <td id="action">{{$data['action']}}</td>
@endforeach
0

my first thought is that you are visiting the / route, which doesn't have the variable all_data. to fix this just call the controller from this route as well: Route::get('/', 'GetApplicationStatusController@getStatus')

0

try this:
return view('welcome')->with(compact('all_data'));

or this:
return view('welcome')->with(['all_data'=>$all_data])