<?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>Forem: Md Parvez Ahmmed</title>
    <description>The latest articles on Forem by Md Parvez Ahmmed (@mdparvez45).</description>
    <link>https://forem.com/mdparvez45</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%2F1181163%2F2f55ed87-ec63-4332-b4b9-25edd5ea8164.jpeg</url>
      <title>Forem: Md Parvez Ahmmed</title>
      <link>https://forem.com/mdparvez45</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mdparvez45"/>
    <language>en</language>
    <item>
      <title>PHP - What are Interfaces?</title>
      <dc:creator>Md Parvez Ahmmed</dc:creator>
      <pubDate>Sat, 23 Dec 2023 05:48:16 +0000</pubDate>
      <link>https://forem.com/mdparvez45/php-what-are-interfaces-jch</link>
      <guid>https://forem.com/mdparvez45/php-what-are-interfaces-jch</guid>
      <description>&lt;p&gt;Interfaces allow you to specify and pre-declare what methods a class should implement.&lt;br&gt;
Interfaces make it easy to use various classes in the same way. When one or more classes use the same interface, it is called "polymorphism".&lt;br&gt;
Interfaces are declared with the &lt;code&gt;interface&lt;/code&gt; keyword before the Interfaces name:&lt;br&gt;
&lt;strong&gt;Example Below:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?PHP
//interface definition
interface Animal {
  public function makeSound();
}
//interface implements is another class
class Cat implements Animal {
  public function makeSound() {
    echo "Meow";
  }
}

$animal = new Cat();
$animal-&amp;gt;makeSound();
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the example above, let's say that we would like to write software that manages a group of animals. There are actions that all of the animals can do, but each animal does it in its way.&lt;br&gt;
&lt;strong&gt;Using interfaces, we can write some code which can work for all of the animals even if each animal behaves differently:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
// Interface definition
interface Animal {
  public function makeSound();
}

// Class definitions with implement interface
class Cat implements Animal {
  public function makeSound() {
    echo " Meow ";
  }
}

class Dog implements Animal {
  public function makeSound() {
    echo " Bark ";
  }
}

class Mouse implements Animal {
  public function makeSound() {
    echo " Squeak ";
  }
}

// Create a list of animals
$cat = new Cat();
$dog = new Dog();
$mouse = new Mouse();
//all animals/classes are to be stored in an array.
$animals = array($cat, $dog, $mouse);

// Tell the animals to make a sound
foreach($animals as $animal) {
  $animal-&amp;gt;makeSound();
}
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example Explained&lt;/strong&gt;&lt;br&gt;
Cat, Dog and Mouse are all classes that implement the Animal interface, which means that all of them can make a sound using the makeSound() method. Because of this, we can loop through all of the animals and tell them to make a sound even if we don't know what type of animal each one is.&lt;/p&gt;

&lt;p&gt;Since the interface does not tell the classes how to implement the method, each animal can make a sound in its way.&lt;/p&gt;

</description>
      <category>php</category>
      <category>oop</category>
      <category>corephp</category>
      <category>programming</category>
    </item>
    <item>
      <title>Crafting a Laravel Search Macro: A Quick Guide</title>
      <dc:creator>Md Parvez Ahmmed</dc:creator>
      <pubDate>Wed, 13 Dec 2023 07:33:36 +0000</pubDate>
      <link>https://forem.com/mdparvez45/crafting-a-laravel-search-macro-a-quick-guide-2jic</link>
      <guid>https://forem.com/mdparvez45/crafting-a-laravel-search-macro-a-quick-guide-2jic</guid>
      <description>&lt;p&gt;Macro is a powerful Laravel framework feature.You can apply Laravel Macros to expand on the functionality of Laravel components.The Macroable trait allows macros to be defined on any class.&lt;/p&gt;

&lt;p&gt;Where can I apply a macro?&lt;br&gt;
If you're doing the same thing over and over again with Laravel components in your system, think about using a macro. It makes your code clearer and easier to reuse.&lt;/p&gt;

&lt;p&gt;For example, let's make a macro that adds a search feature to every model in our Laravel project.&lt;/p&gt;

&lt;p&gt;Integration Steps:&lt;/p&gt;

&lt;p&gt;Step 1: Install Laravel and Create a New Project&lt;br&gt;
First, make sure you have Laravel installed on your computer. If not, you can install it by following the instructions in the official Laravel documentation. After installing Laravel, open your terminal or command prompt and type the following command to create a new Laravel project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer create-project laravel/laravel MacrosApp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you already have an existing Laravel project, you can use it to build macros and enhance its functionality. Simply navigate to your project's root directory in the terminal and proceed with the following steps.&lt;br&gt;
Step 2: Migrate &amp;amp; Seed the Database&lt;br&gt;
Now that you've set up your Laravel project, the next step is to migrate and seed the database. This will create the necessary tables and populate them with sample data.&lt;/p&gt;

&lt;p&gt;(a)Seed the Database:&lt;br&gt;
Open the database/seeders/DatabaseSeeder.php file. You'll find code that seeds the database with sample data. Uncomment this code by removing the // at the beginning of the lines.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;


class DatabaseSeeder extends Seeder
{
    public function run(): void {
        \App\Models\User::factory(10)-&amp;gt;create(); //Uncomment this line
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(b)Run Migrations &amp;amp; Seed:&lt;br&gt;
Open your terminal or command prompt, navigate to your project's root directory, and execute the following command:&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 migrate --seed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 3: Create a Search Macro&lt;br&gt;
To enhance search functionality in your Laravel project, we'll add a custom search macro. Open the App\Providers\AppServiceProvider file and define the macro within the boot method.&lt;br&gt;
(a)Add a Simple Search Macro for a Single Attribute&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use Illuminate\Database\Eloquent\Builder;

//...
public function boot(): void
    {
       Builder::macro('search', function(string $attribute, string $searchTerm) {
                    return $this-&amp;gt;where($attribute, 'LIKE', "%{$searchTerm}%");
        });
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(b)Extend the Search Macro to Handle Multiple Attributes.&lt;br&gt;
To accommodate searching across multiple attributes, we'll modify the search macro to accept an array of attributes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use Illuminate\Support\Arr;
use Illuminate\Database\Eloquent\Builder;


public function boot(): void
    {
       Builder::macro('search', function($attributes, string $searchTerm) {
        foreach(Arr::wrap($attributes) as $attribute) {
            $this-&amp;gt;orWhere($attribute, 'LIKE', "%{$searchTerm}%");
        }
        return $this;
        });
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This extended search macro allows you to specify an array of attributes, making it more versatile for searching across multiple fields. The orWhere method is used to build a query that matches any of the specified attributes with the given search term.&lt;/p&gt;

&lt;p&gt;Step 4 : How to apply the macro for searching&lt;br&gt;
Now, use the following macro to add a search to not just the User model but all models:&lt;br&gt;
Open the routes/web.php file in your project. Add the following code to demonstrate using the search macro in routes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php


use App\Models\User;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    // searching a single column
    return User::search('email','example.com')-&amp;gt;get();

   // searching on multiple columns
   return App\Models\User::search('name', 'mr')-&amp;gt;search('email', 'example.org')-&amp;gt;get();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we are using the search macro to find users by email and perform a combined search by name and email. Adjust the attribute names and search terms based on your actual model attributes.&lt;/p&gt;

&lt;p&gt;Conclusion:&lt;br&gt;
To sum it up, this guide shows how to use Laravel macros to make searching in your project easier. With a simple setup process and a search macro that works for one or multiple attributes, your code becomes cleaner and more adaptable. By following the steps and applying the search macro to your controllers &amp;amp; routes, you can quickly add search features to your models, improving your code's simplicity and efficiency.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>developer</category>
    </item>
  </channel>
</rss>
