{"id":10076,"date":"2021-06-07T11:10:35","date_gmt":"2021-06-07T02:10:35","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=10076"},"modified":"2021-06-07T11:18:36","modified_gmt":"2021-06-07T02:18:36","slug":"laravel-adding-custom-data-to-eloquent-result","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/10076","title":{"rendered":"[Laravel] Adding custom data to eloquent result"},"content":{"rendered":"\n<p>Sometimes, we want to add more custom data to our eloquent result before passing to blade or doing further logic improvements. Today we will be focusing how to add custom data to our eloquent result.<\/p>\n\n\n\n<p>I assume you already know laravel framework in this article as we will not covering from scratch of laravel framework. First let&#8217;s make a <strong>posts<\/strong> table. Here is the migration file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Schema::create('posts', function (Blueprint $table) {\n  $table-&gt;id();\n  $table-&gt;string('name');\n  $table-&gt;string('description');\n  $table-&gt;timestamps();\n});<\/code><\/pre>\n\n\n\n<p>In our post table, we will have these columns in above. And we gonna add a data record. You can add manually using database clients or you can of course make a seeder class to add dummy data like this<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DB::table('posts')-&gt;insert([\n  'name' =&gt; Str::random(10),\n  'description' =&gt; Str::random(100),\n  'created_at' =&gt; date('Y-m-d H:i:s'),\n]);<\/code><\/pre>\n\n\n\n<p>Lets go to our controller and select the data with <strong>all<\/strong> method and print the result with <strong>dd <\/strong>function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$data = Post::all();\ndd($data);<\/code><\/pre>\n\n\n\n<p>The output will be look like this.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"989\" height=\"1024\" src=\"\/appnews\/wp-content\/uploads\/sites\/4\/2021\/06\/image-1-989x1024.png\" alt=\"\" class=\"wp-image-10078\" srcset=\"https:\/\/www.gigas-jp.com\/appnews\/wp-content\/uploads\/sites\/4\/2021\/06\/image-1-989x1024.png 989w, https:\/\/www.gigas-jp.com\/appnews\/wp-content\/uploads\/sites\/4\/2021\/06\/image-1.png 1082w\" sizes=\"(max-width: 989px) 100vw, 989px\" \/><\/figure>\n\n\n\n<p>Now , let&#8217;s try to add a new column called <code>category_id<\/code> and <code>user_id<\/code> to that eloquent results. We can achieve this using <strong>map<\/strong> function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$data = Post::all();\n\n$data-&gt;map(function ($result) {\n  $result-&gt;category_id = 1;\n  $result-&gt;user_id = 2;\n  return $result;\n});\n\ndd($data);<\/code><\/pre>\n\n\n\n<p>And the result will be look like this. Please remember there are two arrays called <code>attributes<\/code> which is the one we customized and added the data. And there is also <code>original<\/code> array which contains only original data from eloquent. <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1024\" height=\"994\" src=\"\/appnews\/wp-content\/uploads\/sites\/4\/2021\/06\/image-3-1024x994.png\" alt=\"\" class=\"wp-image-10080\" srcset=\"https:\/\/www.gigas-jp.com\/appnews\/wp-content\/uploads\/sites\/4\/2021\/06\/image-3-1024x994.png 1024w, https:\/\/www.gigas-jp.com\/appnews\/wp-content\/uploads\/sites\/4\/2021\/06\/image-3.png 1350w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Anyhow, we can access our created data using map like always. For further more to customize the eloquent results , please check out <a href=\"https:\/\/laravel.com\/docs\/5.4\/collections#method-map\">here<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dd($data[0]-&gt;category_id.','.$data[0]-&gt;user_id);\n\/\/result -&gt; 1,2<\/code><\/pre>\n\n\n\n<p>If you need to loop through many data, you can use loop functions but in this case I just added a 0 index to get data easily.<\/p>\n\n\n\n<p>By Yuuma<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10076\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/10076\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"[Laravel] Adding custom data to eloquent result\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10076\" data-text=\"[Laravel] Adding custom data to eloquent result\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10076\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10076\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, we want to add more custom data to our eloquent result before passing to blade or doing further log [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10076"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=10076"}],"version-history":[{"count":6,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10076\/revisions"}],"predecessor-version":[{"id":10089,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10076\/revisions\/10089"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=10076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=10076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=10076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}