<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Reza Rabbani</title>
    <description>The latest articles on DEV Community by Reza Rabbani (@thrashzone13).</description>
    <link>https://dev.to/thrashzone13</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F579488%2Fdc0758d1-c167-456a-87ed-702575556aa4.jpeg</url>
      <title>DEV Community: Reza Rabbani</title>
      <link>https://dev.to/thrashzone13</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thrashzone13"/>
    <language>en</language>
    <item>
      <title>Chunk multidimensional array by value in Laravel</title>
      <dc:creator>Reza Rabbani</dc:creator>
      <pubDate>Mon, 19 Dec 2022 19:37:58 +0000</pubDate>
      <link>https://dev.to/thrashzone13/chunk-multidimensional-array-by-value-in-laravel-476m</link>
      <guid>https://dev.to/thrashzone13/chunk-multidimensional-array-by-value-in-laravel-476m</guid>
      <description>&lt;p&gt;Have you ever needed to put arrays with same value together? It's super easy using collections in Laravel!&lt;/p&gt;

&lt;p&gt;Just imagine you have the following array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$people = [
    ['name' =&amp;gt; 'Alex', 'age' =&amp;gt; 25],
    ['name' =&amp;gt; 'Martin', 'age' =&amp;gt; 32],
    ['name' =&amp;gt; 'John', 'age' =&amp;gt; 25]
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And want to put people with same age into same groups. Using collection in Laravel you can do it in one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$chunkedByAge = collect($people)-&amp;gt;chunkWhile(fn($v, $k, $c) =&amp;gt; $v['age'] == $c-&amp;gt;last()['age']);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Happy coding! &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>tooling</category>
    </item>
    <item>
      <title>Use Hydrator in Laravel Doctrine</title>
      <dc:creator>Reza Rabbani</dc:creator>
      <pubDate>Fri, 29 Oct 2021 16:09:03 +0000</pubDate>
      <link>https://dev.to/thrashzone13/use-hydration-in-laravel-doctrine-36ae</link>
      <guid>https://dev.to/thrashzone13/use-hydration-in-laravel-doctrine-36ae</guid>
      <description>&lt;p&gt;Have you ever used Doctrine in Laravel? If so, this should be familier to you.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** @ORM\Entity **/
class Book {

    /** @ORM\Column(type="string") **/
    protected $title;

    public function getTitle() {
        return $this-&amp;gt;title;
    }

    public function setTitle($title) {
        $this-&amp;gt;title = $title;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just imagine an entity with more properties and in order of that, more setter methods. calling all of them one by one to store a new entity would be a headache.&lt;br&gt;
Wouldn't it be nice if there was something that would handle it based on the keys of the data array that you have? The good news is that it's already done! but there is a lack of documentation in for using it in Laravel.&lt;/p&gt;
&lt;h2&gt;
  
  
  Install Requirements
&lt;/h2&gt;

&lt;p&gt;First of all you should install doctrine in your laravel project. It's well explained &lt;a href="http://www.laraveldoctrine.org/"&gt;here&lt;/a&gt;. Then install doctrine hydrator&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require doctrine/doctrine-laminas-hydrator
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if you want to read more about it, checkout the &lt;a href="https://github.com/doctrine/doctrine-laminas-hydrator"&gt;documentation&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Configure Hydrator
&lt;/h2&gt;

&lt;p&gt;Add doctrine config file to configs directory if you haven't already&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan vendor:publish --tag="config"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;config\doctrine.php&lt;/code&gt; register hydrator inside &lt;code&gt;custom_hydration_modes&lt;/code&gt; key&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'custom_hydration_modes'     =&amp;gt; [
    'default' =&amp;gt; \Doctrine\Laminas\Hydrator\DoctrineObject::class,
],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to use it
&lt;/h2&gt;

&lt;p&gt;Now imagine a BookController&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class BookController {

    protected $em;

    public function __constructor(EntityManagerInterface $em) 
    {
        $this-&amp;gt;em = $em;
    }

    public function store(Request $request) {
        // It will find the proper setter method based on the key and passes the value to that
        $book = $this-&amp;gt;em-&amp;gt;newHydrator('default')-&amp;gt;hydrate($request-&amp;gt;all(), new Book);
        $this-&amp;gt;em-&amp;gt;persist($book);
        $this-&amp;gt;em-&amp;gt;flush();

        // whatever ...
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it!&lt;br&gt;
It's much cleaner than calling setter methods one by one and calling new setter methods after adding new columns to our entities.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>doctrine</category>
      <category>doctrinehydrator</category>
    </item>
  </channel>
</rss>
