Append a string when the data is sent as JSON in Laravel Controller
up vote
0
down vote
favorite
What I want to do is to append the string 'string/'
when I send a JSON response.
For example:
When I send data its as a collection like so..
return response()->json("example_json" => $datacollection)
This would display as:
{
"example_json"
[{
"data1": "somedata1",
"data2": "somedata2",
"data3": "somedata3"
}]
}
Now, what I want to do is append a string, for example the 3rd data's variable content:
{
"example_json"
[{
"data1": "somedata1",
"data2": "somedata2",
"data3": "string/somedata3"
}]
}
Is there any kind of method to do this without changing the data permanently in the database? I just want to do this once or twice not change the name of the content in the database only when it is sent to, change its name.
Thankyou very much!
json laravel laravel-5 eloquent
add a comment |
up vote
0
down vote
favorite
What I want to do is to append the string 'string/'
when I send a JSON response.
For example:
When I send data its as a collection like so..
return response()->json("example_json" => $datacollection)
This would display as:
{
"example_json"
[{
"data1": "somedata1",
"data2": "somedata2",
"data3": "somedata3"
}]
}
Now, what I want to do is append a string, for example the 3rd data's variable content:
{
"example_json"
[{
"data1": "somedata1",
"data2": "somedata2",
"data3": "string/somedata3"
}]
}
Is there any kind of method to do this without changing the data permanently in the database? I just want to do this once or twice not change the name of the content in the database only when it is sent to, change its name.
Thankyou very much!
json laravel laravel-5 eloquent
Is$dataCollection
an instance ofIlluminateSupportCollection
?
– adam
Nov 8 at 23:58
Sorry no, so it is data from doing$datacollection = DB::table('database')->get();
– IneedToAskQuestions
Nov 9 at 1:21
Convert into collection first, then add your data, then again parse into JSON.
– Md. Shohan Hossain
Nov 9 at 4:27
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
What I want to do is to append the string 'string/'
when I send a JSON response.
For example:
When I send data its as a collection like so..
return response()->json("example_json" => $datacollection)
This would display as:
{
"example_json"
[{
"data1": "somedata1",
"data2": "somedata2",
"data3": "somedata3"
}]
}
Now, what I want to do is append a string, for example the 3rd data's variable content:
{
"example_json"
[{
"data1": "somedata1",
"data2": "somedata2",
"data3": "string/somedata3"
}]
}
Is there any kind of method to do this without changing the data permanently in the database? I just want to do this once or twice not change the name of the content in the database only when it is sent to, change its name.
Thankyou very much!
json laravel laravel-5 eloquent
What I want to do is to append the string 'string/'
when I send a JSON response.
For example:
When I send data its as a collection like so..
return response()->json("example_json" => $datacollection)
This would display as:
{
"example_json"
[{
"data1": "somedata1",
"data2": "somedata2",
"data3": "somedata3"
}]
}
Now, what I want to do is append a string, for example the 3rd data's variable content:
{
"example_json"
[{
"data1": "somedata1",
"data2": "somedata2",
"data3": "string/somedata3"
}]
}
Is there any kind of method to do this without changing the data permanently in the database? I just want to do this once or twice not change the name of the content in the database only when it is sent to, change its name.
Thankyou very much!
json laravel laravel-5 eloquent
json laravel laravel-5 eloquent
asked Nov 8 at 23:50
IneedToAskQuestions
145
145
Is$dataCollection
an instance ofIlluminateSupportCollection
?
– adam
Nov 8 at 23:58
Sorry no, so it is data from doing$datacollection = DB::table('database')->get();
– IneedToAskQuestions
Nov 9 at 1:21
Convert into collection first, then add your data, then again parse into JSON.
– Md. Shohan Hossain
Nov 9 at 4:27
add a comment |
Is$dataCollection
an instance ofIlluminateSupportCollection
?
– adam
Nov 8 at 23:58
Sorry no, so it is data from doing$datacollection = DB::table('database')->get();
– IneedToAskQuestions
Nov 9 at 1:21
Convert into collection first, then add your data, then again parse into JSON.
– Md. Shohan Hossain
Nov 9 at 4:27
Is
$dataCollection
an instance of IlluminateSupportCollection
?– adam
Nov 8 at 23:58
Is
$dataCollection
an instance of IlluminateSupportCollection
?– adam
Nov 8 at 23:58
Sorry no, so it is data from doing
$datacollection = DB::table('database')->get();
– IneedToAskQuestions
Nov 9 at 1:21
Sorry no, so it is data from doing
$datacollection = DB::table('database')->get();
– IneedToAskQuestions
Nov 9 at 1:21
Convert into collection first, then add your data, then again parse into JSON.
– Md. Shohan Hossain
Nov 9 at 4:27
Convert into collection first, then add your data, then again parse into JSON.
– Md. Shohan Hossain
Nov 9 at 4:27
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
You can use a custom attribute accessor to modify the model's data on retrieval. That way you can modify it when you get the data, but it will not change it when storing data:
https://laravel.com/docs/5.7/eloquent-mutators#defining-an-accessor
E.g.
public function getData3Attribute($value)
{
return 'string/'.$value;
}
Then when you retrieve the model it will look like this:
{
"data1": "somedata1",
"data2": "somedata2",
"data3": "string/somedata3"
}
NOTE: This transformation will happen anywhere you retrieve data3
, including PHP code. If you ONLY want to modify values when sending a JSON response, check out API resources:
https://laravel.com/docs/5.7/eloquent-resources
JSON resource toArray() method:
public function toArray($request)
{
return [
'data1' => $this->data1,
'data2' => $this->data2,
'data3' => 'string/'.$this->data3,
];
}
Thanks! correct answer because you pointed me in the right direction! thankyou :))
– IneedToAskQuestions
Nov 12 at 0:20
Your first example was the one that I used pretty much, thanks again :)
– IneedToAskQuestions
Nov 12 at 0:30
add a comment |
up vote
0
down vote
Looks like you could use map()
here to update the data as needed.
E.g.
$updatedDatacollection = $datacollection->map(function($item, $key){
if($key == 'data3') { // or some other condition ...
return 'string/' . $item; // append a string
}
return $item; // otherwise leave it unchanged
});
This will update the $updatedDatacollection
but leave the database unchanged. Then you can return as JSON like you are already doing.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You can use a custom attribute accessor to modify the model's data on retrieval. That way you can modify it when you get the data, but it will not change it when storing data:
https://laravel.com/docs/5.7/eloquent-mutators#defining-an-accessor
E.g.
public function getData3Attribute($value)
{
return 'string/'.$value;
}
Then when you retrieve the model it will look like this:
{
"data1": "somedata1",
"data2": "somedata2",
"data3": "string/somedata3"
}
NOTE: This transformation will happen anywhere you retrieve data3
, including PHP code. If you ONLY want to modify values when sending a JSON response, check out API resources:
https://laravel.com/docs/5.7/eloquent-resources
JSON resource toArray() method:
public function toArray($request)
{
return [
'data1' => $this->data1,
'data2' => $this->data2,
'data3' => 'string/'.$this->data3,
];
}
Thanks! correct answer because you pointed me in the right direction! thankyou :))
– IneedToAskQuestions
Nov 12 at 0:20
Your first example was the one that I used pretty much, thanks again :)
– IneedToAskQuestions
Nov 12 at 0:30
add a comment |
up vote
0
down vote
accepted
You can use a custom attribute accessor to modify the model's data on retrieval. That way you can modify it when you get the data, but it will not change it when storing data:
https://laravel.com/docs/5.7/eloquent-mutators#defining-an-accessor
E.g.
public function getData3Attribute($value)
{
return 'string/'.$value;
}
Then when you retrieve the model it will look like this:
{
"data1": "somedata1",
"data2": "somedata2",
"data3": "string/somedata3"
}
NOTE: This transformation will happen anywhere you retrieve data3
, including PHP code. If you ONLY want to modify values when sending a JSON response, check out API resources:
https://laravel.com/docs/5.7/eloquent-resources
JSON resource toArray() method:
public function toArray($request)
{
return [
'data1' => $this->data1,
'data2' => $this->data2,
'data3' => 'string/'.$this->data3,
];
}
Thanks! correct answer because you pointed me in the right direction! thankyou :))
– IneedToAskQuestions
Nov 12 at 0:20
Your first example was the one that I used pretty much, thanks again :)
– IneedToAskQuestions
Nov 12 at 0:30
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You can use a custom attribute accessor to modify the model's data on retrieval. That way you can modify it when you get the data, but it will not change it when storing data:
https://laravel.com/docs/5.7/eloquent-mutators#defining-an-accessor
E.g.
public function getData3Attribute($value)
{
return 'string/'.$value;
}
Then when you retrieve the model it will look like this:
{
"data1": "somedata1",
"data2": "somedata2",
"data3": "string/somedata3"
}
NOTE: This transformation will happen anywhere you retrieve data3
, including PHP code. If you ONLY want to modify values when sending a JSON response, check out API resources:
https://laravel.com/docs/5.7/eloquent-resources
JSON resource toArray() method:
public function toArray($request)
{
return [
'data1' => $this->data1,
'data2' => $this->data2,
'data3' => 'string/'.$this->data3,
];
}
You can use a custom attribute accessor to modify the model's data on retrieval. That way you can modify it when you get the data, but it will not change it when storing data:
https://laravel.com/docs/5.7/eloquent-mutators#defining-an-accessor
E.g.
public function getData3Attribute($value)
{
return 'string/'.$value;
}
Then when you retrieve the model it will look like this:
{
"data1": "somedata1",
"data2": "somedata2",
"data3": "string/somedata3"
}
NOTE: This transformation will happen anywhere you retrieve data3
, including PHP code. If you ONLY want to modify values when sending a JSON response, check out API resources:
https://laravel.com/docs/5.7/eloquent-resources
JSON resource toArray() method:
public function toArray($request)
{
return [
'data1' => $this->data1,
'data2' => $this->data2,
'data3' => 'string/'.$this->data3,
];
}
edited Nov 9 at 2:18
answered Nov 9 at 2:10
newUserName02
46838
46838
Thanks! correct answer because you pointed me in the right direction! thankyou :))
– IneedToAskQuestions
Nov 12 at 0:20
Your first example was the one that I used pretty much, thanks again :)
– IneedToAskQuestions
Nov 12 at 0:30
add a comment |
Thanks! correct answer because you pointed me in the right direction! thankyou :))
– IneedToAskQuestions
Nov 12 at 0:20
Your first example was the one that I used pretty much, thanks again :)
– IneedToAskQuestions
Nov 12 at 0:30
Thanks! correct answer because you pointed me in the right direction! thankyou :))
– IneedToAskQuestions
Nov 12 at 0:20
Thanks! correct answer because you pointed me in the right direction! thankyou :))
– IneedToAskQuestions
Nov 12 at 0:20
Your first example was the one that I used pretty much, thanks again :)
– IneedToAskQuestions
Nov 12 at 0:30
Your first example was the one that I used pretty much, thanks again :)
– IneedToAskQuestions
Nov 12 at 0:30
add a comment |
up vote
0
down vote
Looks like you could use map()
here to update the data as needed.
E.g.
$updatedDatacollection = $datacollection->map(function($item, $key){
if($key == 'data3') { // or some other condition ...
return 'string/' . $item; // append a string
}
return $item; // otherwise leave it unchanged
});
This will update the $updatedDatacollection
but leave the database unchanged. Then you can return as JSON like you are already doing.
add a comment |
up vote
0
down vote
Looks like you could use map()
here to update the data as needed.
E.g.
$updatedDatacollection = $datacollection->map(function($item, $key){
if($key == 'data3') { // or some other condition ...
return 'string/' . $item; // append a string
}
return $item; // otherwise leave it unchanged
});
This will update the $updatedDatacollection
but leave the database unchanged. Then you can return as JSON like you are already doing.
add a comment |
up vote
0
down vote
up vote
0
down vote
Looks like you could use map()
here to update the data as needed.
E.g.
$updatedDatacollection = $datacollection->map(function($item, $key){
if($key == 'data3') { // or some other condition ...
return 'string/' . $item; // append a string
}
return $item; // otherwise leave it unchanged
});
This will update the $updatedDatacollection
but leave the database unchanged. Then you can return as JSON like you are already doing.
Looks like you could use map()
here to update the data as needed.
E.g.
$updatedDatacollection = $datacollection->map(function($item, $key){
if($key == 'data3') { // or some other condition ...
return 'string/' . $item; // append a string
}
return $item; // otherwise leave it unchanged
});
This will update the $updatedDatacollection
but leave the database unchanged. Then you can return as JSON like you are already doing.
answered Nov 9 at 2:05
Peter
46119
46119
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217863%2fappend-a-string-when-the-data-is-sent-as-json-in-laravel-controller%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is
$dataCollection
an instance ofIlluminateSupportCollection
?– adam
Nov 8 at 23:58
Sorry no, so it is data from doing
$datacollection = DB::table('database')->get();
– IneedToAskQuestions
Nov 9 at 1:21
Convert into collection first, then add your data, then again parse into JSON.
– Md. Shohan Hossain
Nov 9 at 4:27