php


Laravel formatting JSON output


Is there a simple way to return more objects with different attribute name in JSON response?
For example I have a User model which has attributes name, password, xy and if I do return User::all(); I will return all users. But what if I want to return a list of custom objects where I will return JSON format like:
{
'username' : ...
'pass': ...
'xyz' : ...
'from_related_model' : ...
}
so I will have name of fields returned differently then they are in DB, and also attach to each returned object an attribute from related model.
I don't want to use mutators as I only want to return like that within one controller. I know I can traverse through object with foreach and make a new object by assigning values, and returning merged object. But is there a simpler way to do this?
You'll need to generate the list of objects to return manually.
Example: if you wanted to return a custom object for each user fetched from the database you could do something like the following in your controller:
$result = [];
foreach (User::all() as $user) {
$result[] = [
'username' => $user->username,
'full_name' => $user->first_name . ' ' . $user->last_name,
'xyz' => 5,
];
}
return $result;
Every time you return an array or a Collection from controller method, it will be converted automatically to JSON.

Related Links

php mysql Select data
Rewrite URL but keep external files
How to Check with PHP if someone is uploading something on my FTP server
How to add value in session in array form & fetch the session value in ci?
Why do I get a error 400 trying to insert a CSV file with Google Drive SDK?
PHP if else statement else gives blank screen
Single line prevents entire script from working correctly
Using session and cookies for persistent data
PHP Concatenation to echo script inside of a ternary
preg_replace inside of PHP file
where is Linux driver for PHP accessing MS SQL? [closed]
PHP sort selectpicker
Php, Symfony 1.4,doctrine
Wordpress listen to the event “update” or “move to the trash” the post
variables in $_GET superglobal
foreach to get the array index results in PHP

Categories

HOME
android-studio
apple-push-notifications
bokeh
subdomain
stacktrace.js
plpgsql
filesize
initialization
docker-cloud
lvm
hyperledger
onvif
cisco
fireloop
vimeo
heap-dump
uima
word2vec
opam
sqlplus
eclipse-emf
quartz.net
introspection
plyr
cellular-network
beego
marquee
docx4j
jsonresult
copy-protection
locks
series
knights-tour
netstat
formsauthenticationticket
redgate
grails-spring-security
klee
1wire
jqgrid-asp.net
kitura
music21
sony-future-lab-n
password-hash
head.js
jgrapht
android-mediarecorder
popen
email-forwarding
dblink
mpmovieplayercontroller
nrf51
pbx
svnserve
wininet
jtag
maven-release-plugin
rails-engines
android-async-http
polymerfire
openmrs
trust
mousehover
auto-generate
tree-structure
shoes
android-audiomanager
mutators
dos2unix
dc
accessory
windows-messages
redpitaya
lsa
unrealscript
iirf
insertonsubmit
jquery-lazyload
flv
cmath
dojo-1.9
image-formats
telerik-ajax
office-automation
winsxs
css-friendly
dip
active-record-query
projectpier
rtti
twitter-feed

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App