<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>map() function Archives - Codeplaners</title>
	<atom:link href="https://codeplaners.com/tag/map-function/feed/" rel="self" type="application/rss+xml" />
	<link>https://codeplaners.com/tag/map-function/</link>
	<description>Code Solution</description>
	<lastBuildDate>Wed, 01 Jan 2025 06:40:08 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>

<image>
	<url>https://codeplaners.com/wp-content/uploads/2020/09/cropped-favicon-social-32x32.png</url>
	<title>map() function Archives - Codeplaners</title>
	<link>https://codeplaners.com/tag/map-function/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Laravel 10 map() function Add Attribute Example</title>
		<link>https://codeplaners.com/laravel-10-map-function-add-attribute-example/</link>
					<comments>https://codeplaners.com/laravel-10-map-function-add-attribute-example/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Thu, 07 Nov 2024 03:08:06 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[Laravel 10]]></category>
		<category><![CDATA[Laravel 8]]></category>
		<category><![CDATA[Laravel 9]]></category>
		<category><![CDATA[map() function]]></category>
		<guid isPermaLink="false">https://codeplaners.com/?p=1650</guid>

					<description><![CDATA[<p>This post was last updated on January 1st, 2025 at 06:40 amHi dev, Today, i show you laravel 10 map() function add attribute example. In this article will tell you laravel 10 map() function add attribute example. you will laravel 10 map() function add attribute example. You can also use this example with Laravel 6, &#8230; <a href="https://codeplaners.com/laravel-10-map-function-add-attribute-example/" class="more-link">Continue reading<span class="screen-reader-text"> "Laravel 10 map() function Add Attribute Example"</span></a></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/laravel-10-map-function-add-attribute-example/">Laravel 10 map() function Add Attribute Example</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p class="last-modified">This post was last updated on January 1st, 2025 at 06:40 am</p><p>Hi dev,</p>
<p>Today, i show you laravel 10 map() function add attribute example. In this article will tell you laravel 10 map() function add attribute example. you will laravel 10 map() function add attribute example. </p>
<p>You can also use this example with Laravel 6, Laravel 7, Laravel 8, Laravel 9 and Laravel 10 versions.</p>
<p>If we are posting with status and you need to set label on status like &#8220;0&#8221; means &#8220;Inactive&#8221; and &#8220;1&#8221; means &#8220;Active&#8221;, then you can do it using laravel collection map() function.</p>
<p>So, let’s follow few steps to create example of laravel 10 map() function add attribute example.</p>
<h3 class="step_code">Example 1: setAttribute()</h3>
<pre class="brush: php; title: ; notranslate">
&lt;?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $user = User::first();
  
        $user-&gt;setAttribute('position', 'Web Developer');
  
        dd($user-&gt;toArray());
    }
}
</pre>
<h3 class="step_code">Output:</h3>
<pre class="brush: php; title: ; notranslate">
array:8 &#x5B;

  &quot;id&quot; =&gt; 1

  &quot;name&quot; =&gt; &quot;Mohan&quot;

  &quot;email&quot; =&gt; &quot;codeplaners@gmail.com&quot;

  &quot;email_verified_at&quot; =&gt; null

  &quot;created_at&quot; =&gt; &quot;2022-06-23T07:02:17.000000Z&quot;

  &quot;updated_at&quot; =&gt; &quot;2022-06-23T07:02:17.000000Z&quot;

  &quot;birthdate&quot; =&gt; &quot;1998-11-15&quot;

  &quot;position&quot; =&gt; &quot;Web Developer&quot;

]
</pre>
<h3 class="step_code">Example 2: map()</h3>
<pre class="brush: php; title: ; notranslate">
&lt;?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Illuminate\Support\Str;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $posts = collect(&#x5B;
            &#x5B;'id'=&gt;1,'title'=&gt;'Post One'],
            &#x5B;'id'=&gt;2,'title'=&gt;'Post Two'],
            &#x5B;'id'=&gt;3,'title'=&gt;'Post Three'],
            &#x5B;'id'=&gt;4,'title'=&gt;'Post Four'],
        ]);
   
        $posts = $posts-&gt;map(function ($post) {
            $post&#x5B;'url'] = url(&quot;post/&quot;. Str::slug($post&#x5B;'title']));
            return $post;
        });
  
        dd($posts-&gt;toArray());
    }
}
</pre>
<h3 class="step_code">Output:</h3>
<pre class="brush: php; title: ; notranslate">
Array

(

    &#x5B;0] =&gt; Array

        (

            &#x5B;id] =&gt; 1

            &#x5B;title] =&gt; Post One

            &#x5B;url] =&gt; http://localhost:8000/post/post-one

        )

    &#x5B;1] =&gt; Array

        (

            &#x5B;id] =&gt; 2

            &#x5B;title] =&gt; Post Two

            &#x5B;url] =&gt; http://localhost:8000/post/post-two

        )

    &#x5B;2] =&gt; Array

        (

            &#x5B;id] =&gt; 3

            &#x5B;title] =&gt; Post Three

            &#x5B;url] =&gt; http://localhost:8000/post/post-three

        )

    &#x5B;3] =&gt; Array

        (

            &#x5B;id] =&gt; 4

            &#x5B;title] =&gt; Post Four

            &#x5B;url] =&gt; http://localhost:8000/post/post-four

        )

)
</pre>
<h3 class="step_code">Example 3: Laravel 10 Eloquent Collection Add Attribute using map()</h3>
<pre class="brush: php; title: ; notranslate">
&lt;?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Post;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $posts = Post::select(&quot;id&quot;, &quot;title&quot;, &quot;status&quot;)-&gt;take(5)-&gt;get();
  
        $posts = $posts-&gt;map(function ($post) {
            $post-&gt;status_label = $post-&gt;status == 1 ? 'Active' : 'InActive';
            return $post;
        });
  
        dd($posts-&gt;toArray());
    }
}
</pre>
<h3 class="step_code">Output:</h3>
<pre class="brush: php; title: ; notranslate">
Array

(

    &#x5B;0] =&gt; Array

        (

            &#x5B;id] =&gt; 2

            &#x5B;title] =&gt; Demo Updated

            &#x5B;status] =&gt; 1

            &#x5B;status_label] =&gt; Active

        )

    &#x5B;1] =&gt; Array

        (

            &#x5B;id] =&gt; 3

            &#x5B;title] =&gt; Dr.

            &#x5B;status] =&gt; 0

            &#x5B;status_label] =&gt; InActive

        )

    &#x5B;2] =&gt; Array

        (

            &#x5B;id] =&gt; 4

            &#x5B;title] =&gt; Miss

            &#x5B;status] =&gt; 0

            &#x5B;status_label] =&gt; InActive

        )

    &#x5B;3] =&gt; Array

        (

            &#x5B;id] =&gt; 5

            &#x5B;title] =&gt; Dr.

            &#x5B;status] =&gt; 0

            &#x5B;status_label] =&gt; InActive

        )

    &#x5B;4] =&gt; Array

        (

            &#x5B;id] =&gt; 6

            &#x5B;title] =&gt; Mrs.

            &#x5B;status] =&gt; 0

            &#x5B;status_label] =&gt; InActive

        )

)
</pre>
<p><strong>I hope it will assist you…</strong></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/laravel-10-map-function-add-attribute-example/">Laravel 10 map() function Add Attribute Example</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codeplaners.com/laravel-10-map-function-add-attribute-example/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
