0

I have been developing a web app which collects data from database and print. I am using the Laravel framework. I have shown below the code use. I can display the JSON response directly in the my PHP code, but if I try to iterate through the JSON response I cannot get anything.

Here is how I generate the JSON object using Laravel Facades library:

  $worklogs = Response::json($data, $this->getStatusCode(), $headers);

Here is the code I used to pass the data to the PHP page:

  return view('index')->with('worklogs', $worklogs);

I can print the collected data from the PHP page using the following code:

  {{ $worklogs }}

But if I try to iterate through it using the foreach loop, an error occurs as shown:

error captured

In the PHP page this is how I coded it, but it is not working: captured index.blade.php file portion

4
  • You don't need to json encode it in the first place. Commented May 30, 2016 at 18:39
  • 1
    Why You're using Response::json and return view() i really don't get it? Commented May 30, 2016 at 19:09
  • Posting images of your code is less useful than posting the code itself as text. Commented May 30, 2016 at 22:33
  • You don't need to use Response facade unless you are returning it to user/browser. Try return view('index')->with('worklogs', $data); instead - just pass your original $data array to the view Commented May 31, 2016 at 1:11

2 Answers 2

1

I'm not sure I understand .. first of all this is how you return a json:

return Response::json([
    'hello' => $value
], $headers);

Second: what do you mean iterate the jSon? Third: your error messages state that Undefined variable $datas .. not $data, $datas. are you sending the $data param to the view?

Sign up to request clarification or add additional context in comments.

Comments

0

First and foremost as Bogdan Bocioaca said in his answer you only use the Response::json function when you want to return Json only(mostly for APIs so that other apps can access your json data). For example the code below would return JSON which might be read by a mobile app

$worklogs = WorkLog::all();
return Response::json([
    'worklogs' => $worklogs
], $headers);

By my understanding of your question you want to pass the data directly to the view, so your code should ideally be as follows:

$worklogs = WorkLog::all();
return view('index',compact('worklogs'));

(Note that above i chose to use the compact() instead of with->() because its cleaner)

Then you can iterate over your code in the as you wanted:

@foreach($worklogs as $worklog)
  <td>{{$worklog->id}}</td>
  <td>{{$worklog->name}}</td>
  <td>{{$worklog->company}}</td>
@endforeach

2 Comments

i thought i have tried your way but i guess i must have had something wrong before.
well i was making an API which returns JSON. i am making web and mobile app. both are using these API. so i wanted to use the returned json for both the view and mobile app. So i wanted to convert the json for the my view. but is it a proper/secure way to pass data directly the database array. keep in mind that i am allowing a certain data to pass to the model. One thing i was trying was putting such collected data to the Model and pass the model for the view, so is these better way?

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.