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!










share|improve this question






















  • 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










  • Convert into collection first, then add your data, then again parse into JSON.
    – Md. Shohan Hossain
    Nov 9 at 4:27















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!










share|improve this question






















  • 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










  • Convert into collection first, then add your data, then again parse into JSON.
    – Md. Shohan Hossain
    Nov 9 at 4:27













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!










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 23:50









IneedToAskQuestions

145




145












  • 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










  • 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












  • 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












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,
];
}





share|improve this answer























  • 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


















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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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,
    ];
    }





    share|improve this answer























    • 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















    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,
    ];
    }





    share|improve this answer























    • 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













    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,
    ];
    }





    share|improve this answer














    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,
    ];
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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


















    • 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












    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 2:05









        Peter

        46119




        46119






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff