0

I have to create a custom json object (I need to decide what info goes where in what order) with data I get from PHP in my Controller.


As I see it, I think I need some roundtrips like this:

  1. Get data from Laravel-form in my Controller (Input::get('name'))
  2. Pass that data to JavaScript to build my JSON
  3. Pass that JSON back to PHP
  4. Convert this JSON to a string (to store it in Redis)

And when I try to read this data go the other way around:

  1. Get data from DB
  2. Pass data to JavaScript to get each element/node
  3. Parse those values to html (to display the data from the json)

How would I pass variables from PHP to JS and back? Not sure how that works... A good explanation (with some code or example) would be much appreciated!!!

Or is there another 'better' way?

I am using Laravel 5.2 and NoSQL Redis. I need to create a somewhat complex JSON that looks like this and store that in Redis. This JSON will never be more than twice the size of this one shown here.

Thanks

1 Answer 1

1

to generate json data, you can do that with json_encode function in php. No need to pass to javascript to build json data. http://php.net/manual/en/function.json-encode.php

update:

you can store the response of json_encode function in a variable and you can do whatever you want with that variable

$json_data=json_encode($data);
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, this seems to work.. But now I have this huge (150+ lines) block of json inmy Controller. s there a way to set this is a seperate file and call it in the Controller (to clear up my Controller clutter)?
Yes I see you point. But since I build my own JSON structure (not imported from sql table) I have this json variable in my Controlller. Can you make a variable that is useable across the whole project?
could you paste your controller code ? You can declare global variable so that it is accessible across the whole project
Not sure if it will be managable, since I will need to pass about 20 variables to that global variable.. Now I have only done it on line 98. gist.github.com/NicolasVanhecke/e972147b43f8bd03c0b6
I see you have hard coded a json object to php string variable. It would be better to create an array which holds your key value pairs. Then use json_encode($arrayVariable) to convert array to json.
|

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.