<?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: AJ</title>
    <description>The latest articles on Forem by AJ (@aj_c6413caf1a793de3a2163b).</description>
    <link>https://forem.com/aj_c6413caf1a793de3a2163b</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%2F2114731%2F1457319c-11b7-4931-af7a-002f1736f994.png</url>
      <title>Forem: AJ</title>
      <link>https://forem.com/aj_c6413caf1a793de3a2163b</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aj_c6413caf1a793de3a2163b"/>
    <language>en</language>
    <item>
      <title>What is Type Casting in laravel</title>
      <dc:creator>AJ</dc:creator>
      <pubDate>Mon, 30 Sep 2024 15:35:44 +0000</pubDate>
      <link>https://forem.com/aj_c6413caf1a793de3a2163b/what-is-type-casting-in-laravel-oj8</link>
      <guid>https://forem.com/aj_c6413caf1a793de3a2163b/what-is-type-casting-in-laravel-oj8</guid>
      <description>&lt;p&gt;&lt;a href="https://dev.to/aj_c6413caf1a793de3a2163b/laravel-type-casting-44d0"&gt;&lt;/a&gt;You can specify the data types for certain model attributes in Laravel by using type casting. It makes sure that Laravel automatically casts the attribute values to the designated types when you retrieve data from the database.&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>php</category>
      <category>laravel</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Laravel Type Casting</title>
      <dc:creator>AJ</dc:creator>
      <pubDate>Fri, 27 Sep 2024 15:29:40 +0000</pubDate>
      <link>https://forem.com/aj_c6413caf1a793de3a2163b/laravel-type-casting-44d0</link>
      <guid>https://forem.com/aj_c6413caf1a793de3a2163b/laravel-type-casting-44d0</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;You can specify the data types for certain model attributes in Laravel by using type casting. It makes sure that Laravel automatically casts the attribute values to the designated types when you retrieve data from the database.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You Can Check Here For multiple types of Attribute Casting&lt;/p&gt;

&lt;p&gt;By default, Laravel provides several cast types that you can use:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;Integer&lt;/code&gt;: The attribute will be cast to an integer.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Real&lt;/code&gt;: The attribute will be cast to a float.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Float&lt;/code&gt;: The attribute will be cast to a float.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Double&lt;/code&gt;: The attribute will be cast to a double.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;String&lt;/code&gt;: The attribute will be cast to a string.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Boolean&lt;/code&gt;: The attribute will be cast to a boolean.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Object&lt;/code&gt;: The attribute will be cast to a PHP object.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Array&lt;/code&gt;: The attribute will be cast to a PHP array.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Collection&lt;/code&gt;: The attribute will be cast to a Laravel collection.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Date&lt;/code&gt;: The attribute will be cast to a date (Y-m-d) format.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DateTime&lt;/code&gt;: The attribute will be cast to a DateTime instance.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Timestamp&lt;/code&gt;: The attribute will be cast to a Unix timestamp (integer).
&lt;/li&gt;
&lt;/ol&gt;

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

    use Illuminate\Database\Eloquent\Model;

    class User extends Model
    {
        /**
         * The attributes that should be cast.
         *
         * @var array
         */
        protected $casts = [
            'is_admin' =&amp;gt; 'boolean',
            'age' =&amp;gt; 'integer',
            'data' =&amp;gt; 'array',
            'created_at' =&amp;gt; 'datetime',
        ];
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this illustration, the created_at value will be converted to a DateTime instance, the age attribute to an integer, the data attribute to an array, and the is_admin attribute to a boolean.&lt;/p&gt;

&lt;p&gt;Therefore, you can keep JSON tags data in a user table, but when you fetch the users, you can immediately transform them into a PHP array, which eliminates the need to create a tags table.&lt;/p&gt;

&lt;p&gt;When working with attributes in your Laravel models, type casting makes it easier to deal with the desired data type without having to convert it every time you access or change an attribute’s value.&lt;/p&gt;




&lt;p&gt;if you love the content and want to support more awesome articles, consider buying me a coffee! ☕️🥳 Your support means the world to me and helps keep the knowledge flowing. You can do that right here: 👉 &lt;a href="https://buymeacoffee.com/jainaman" rel="noopener noreferrer"&gt;Buy Me a Coffee&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Create a Chrome Extension Using JavaScript: A Beginner’s Guide</title>
      <dc:creator>AJ</dc:creator>
      <pubDate>Thu, 26 Sep 2024 03:12:19 +0000</pubDate>
      <link>https://forem.com/aj_c6413caf1a793de3a2163b/how-to-create-a-chrome-extension-using-javascript-a-beginners-guide-4pfc</link>
      <guid>https://forem.com/aj_c6413caf1a793de3a2163b/how-to-create-a-chrome-extension-using-javascript-a-beginners-guide-4pfc</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Imagine having the power to add new features or modify any website to suit your needs, directly in your browser. What if you could instantly block ads, modify the look of a page, or even integrate superhero-themed functionality? In this guide, I’ll walk you through creating your first Chrome extension using JavaScript — no superpowers required! Whether you’re a beginner or a seasoned developer, you will find that Chrome extensions are a fun way to flex your JS skills.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fknr36thcihdkk3veeyzs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fknr36thcihdkk3veeyzs.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here’s an outline for the article:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;What is Chrome Extensions?&lt;/li&gt;
&lt;li&gt;What You Will Need&lt;/li&gt;
&lt;li&gt;Setting up the Chrome Extension&lt;/li&gt;
&lt;li&gt;Creating the Manifest File&lt;/li&gt;
&lt;li&gt;Writing the Background Script&lt;/li&gt;
&lt;li&gt;Creating the Popup HTML and Popup js Script&lt;/li&gt;
&lt;li&gt;Implementing the Content Script&lt;/li&gt;
&lt;li&gt;Testing the Extension
10.Real-World Example: My First Chrome Extension&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Are you prepared to enhance your online browsing experience? We will walk you through the process of building a Chrome extension that always opens your browser with a friendly welcome message. This course will assist you in understanding the fundamentals of developing Chrome extensions using JavaScript, regardless of your level of experience as a developer.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Chrome Extension?
&lt;/h2&gt;

&lt;p&gt;Let’s start with the fundamentals before digging into the code. Chrome extensions are little apps that improve your browser’s capabilities. These extensions, which are made with common web technologies like HTML, CSS, and JavaScript, let users personalize their browsing in unexpected ways.&lt;/p&gt;

&lt;h2&gt;
  
  
  What You Will Need:
&lt;/h2&gt;

&lt;p&gt;A basic understanding of JavaScript&lt;br&gt;
Google Chrome installed on your computer&lt;br&gt;
A text editor (like VSCode)&lt;br&gt;
Let’s dive in!&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting up the Chrome Extension
&lt;/h2&gt;

&lt;p&gt;To get started, create a new folder on your computer. This folder will hold all the files related to the Chrome extension. Let’s call it chrome-extension. Inside this folder, you will need two important files:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;manifest.json&lt;/code&gt; (the brain of your extension)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;background.js&lt;/code&gt; (the script that runs in the background).
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Here’s how to structure it:

chrome-extension/
    ├── manifest.json
    └── background.js
    └── popup.html
    └── popup.js
    └── contentScript.js

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Creating the manifest.json File
&lt;/h2&gt;

&lt;p&gt;The manifest file is the heart of any Chrome extension. It defines the extension’s properties, permissions, and behavior. Let’s create a manifest.json file in our project root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "manifest_version": 3,
    "name": "Random Background Color",
    "version": "1.0",
    "description": "A Chrome extension that changes the background color of a webpage to a random color",
    "permissions": ["activeTab"],
    "action": {
      "default_popup": "popup.html"
    },
    "content_scripts": [
      {
        "matches": ["&amp;lt;all_urls&amp;gt;"],
        "js": ["contentScript.js"]
      }
    ]
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to know more about the manifest then you can check this &lt;a href="https://developer.chrome.com/docs/extensions/reference/manifest" rel="noopener noreferrer"&gt;Manifest Format&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;manifest_version:&lt;/code&gt; 3: This specifies that we are using version 3 of the manifest. Always ensure we are using the latest version.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;name:&lt;/code&gt; The name of your extension. This will appear in the Chrome extension manager.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;version:&lt;/code&gt; This is your extension’s version. Keep this updated as you add more features.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;description:&lt;/code&gt; A short description of what your extension does.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;permissions:&lt;/code&gt; Grants permission to access and modify the currently active tab in the browser.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;action:&lt;/code&gt; Specifies the popup window (popup.html) that opens when the extension icon is clicked in the browser toolbar.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;content_scripts:&lt;/code&gt; Injects contentScript.js into every webpage visited ( allows it on all sites). The script likely changes the webpage’s background color.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Writing the background Script
&lt;/h2&gt;

&lt;p&gt;The background script is responsible for handling events like opening tabs or listening for changes in browser state. Let’s create a basic &lt;code&gt;background.js&lt;/code&gt; script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chrome.runtime.onInstalled.addListener(() =&amp;gt; {
  console.log("Extension installed!");
});

chrome.action.onClicked.addListener((tab) =&amp;gt; {
  console.log("Extension icon clicked");
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Now breakdown the background.js
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;onInstalled&lt;/code&gt;: This event is triggered when the extension is installed or updated.&lt;br&gt;
&lt;code&gt;onClicked&lt;/code&gt;: This event is fired when the user clicks on the extension’s icon.&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating the Popup HTML and Popup js Script
&lt;/h2&gt;

&lt;p&gt;The popup is the small window that appears when the user clicks on your extension’s icon. Create a popup.html file like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// popup.html

&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Random Background Color&amp;lt;/title&amp;gt;
    &amp;lt;style&amp;gt;
      body {
        width: 200px;
        height: 100px;
        font-family: Arial, sans-serif;
        text-align: center;
      }
    &amp;lt;/style&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Random Background Color&amp;lt;/h1&amp;gt;
    &amp;lt;button id="change-color"&amp;gt;Change Color&amp;lt;/button&amp;gt;
    &amp;lt;script src="popup.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In&lt;code&gt;popup.js&lt;/code&gt;, add a script to handle the button click:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.addEventListener("DOMContentLoaded", function () {
    const changeColorButton = document.getElementById("change-color");
    changeColorButton.addEventListener("click", function () {
      chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
        chrome.tabs.sendMessage(tabs[0].id, { action: "change-color" });
      });
    });
  });

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This script listens for the page’s DOMContentLoaded event, meaning it waits until the HTML document is fully loaded and parsed before running. Once the page is ready:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It selects the button with the ID &lt;code&gt;change-color&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It adds a &lt;code&gt;click&lt;/code&gt; event listener to this button.&lt;/li&gt;
&lt;li&gt;When the button is clicked, the script uses &lt;code&gt;chrome.tabs.query&lt;/code&gt; to find the currently active tab in the browser window.&lt;/li&gt;
&lt;li&gt;After identifying the active tab, it sends a message to the content script of that tab with an action, &lt;code&gt;change-color&lt;/code&gt; using &lt;code&gt;chrome.tabs.sendMessage&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Content Script
&lt;/h2&gt;

&lt;p&gt;A content script allows your extension to interact with web pages. Create a content.js file to manipulate or extract data from the web page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getRandomColor() {
    const letters = "0123456789ABCDEF";
    let color = "#";
    for (let i = 0; i &amp;lt; 6; i++) {
      color += letters[Math.floor(Math.random() * 16)];
    }
    return color;
  }

  chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    if (request.action === "change-color") {
      const color = getRandomColor();
      document.body.style.backgroundColor = color;
     sendResponse({ status: "Background changed!" });
    }
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s how it works step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;chrome.runtime.onMessage.addListener()&lt;/code&gt;: This is a function that listens for messages sent from other parts of the Chrome extension, such as background scripts or popup scripts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Callback Parameters:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;request&lt;/code&gt;: The message sent by another part of the extension. It contains data, like what action to perform.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;sender&lt;/code&gt;: Information about who sent the message.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;sendResponse&lt;/code&gt;: A function to send a response back to the message sender.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Message Handling&lt;/code&gt;: Inside the callback, it checks if the &lt;code&gt;request.action&lt;/code&gt; is &lt;code&gt;change-color&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Action&lt;/code&gt;: If the action is &lt;code&gt;change-color&lt;/code&gt;, it changes the background color of the web page's body to random color.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Response&lt;/code&gt;: After changing the background color, it sends a response back to the sender with the message &lt;code&gt;{ status: "Background changed!" }&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Testing the Extension
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Open Chrome and go to &lt;code&gt;chrome://extensions/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Enable Developer Mode&lt;/code&gt; (toggle in the top right corner).&lt;/li&gt;
&lt;li&gt;Click Load Unpacked and select your extension’s folder.&lt;/li&gt;
&lt;li&gt;Your extension should now appear in the list! Click the icon in the browser toolbar to test the popup and scripts.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Add More Features
&lt;/h2&gt;

&lt;p&gt;This is just the start! You can enhance your extension by adding new features like:&lt;/p&gt;

&lt;p&gt;To learn more about &lt;strong&gt;storage&lt;/strong&gt;, &lt;strong&gt;permissions&lt;/strong&gt;, and &lt;strong&gt;options page&lt;/strong&gt; for Chrome extensions, you can refer to the following documentation:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Storage: Use chrome.storage to save user preferences. &lt;a href="https://developer.chrome.com/docs/extensions/reference/api/storage" rel="noopener noreferrer"&gt;&lt;strong&gt;chrome.storage API&lt;/strong&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Permissions: Request access to additional Chrome features (like bookmarks, and history). &lt;a href="https://developer.chrome.com/docs/extensions/develop/concepts/declare-permissions" rel="noopener noreferrer"&gt;&lt;strong&gt;Permission API&lt;/strong&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Options Page: Add an options page to allow users to configure the extension. &lt;a href="https://developer.chrome.com/docs/extensions/develop/ui/options-page" rel="noopener noreferrer"&gt;&lt;strong&gt;Options Page&lt;/strong&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;For more details, visit the&lt;a href="https://developer.chrome.com/docs/extensions/get-started" rel="noopener noreferrer"&gt; &lt;strong&gt;Chrome Extensions documentation&lt;/strong&gt;.&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Real-World Example: My First Chrome Extension
&lt;/h2&gt;

&lt;p&gt;When I created my first Chrome extension, I was eager to make my browsing experience more personalized. The idea of a browser welcoming me whenever I opened it felt like adding a touch of personality to my digital routine. It was the simplest idea, but the satisfaction of seeing it work was huge. For developers just getting started, these small wins are what keep us pushing forward.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion: Make It Yours.
&lt;/h2&gt;

&lt;p&gt;Chrome extensions are powerful tools that can enhance your browser experience. This tutorial is just the beginning — once you’re comfortable with the basics, you can extend this project further by adding custom buttons, integrating APIs, or even storing user preferences.&lt;/p&gt;




</description>
      <category>extensions</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Laravel Local Scopes</title>
      <dc:creator>AJ</dc:creator>
      <pubDate>Tue, 24 Sep 2024 13:50:44 +0000</pubDate>
      <link>https://forem.com/aj_c6413caf1a793de3a2163b/laravel-local-scopes-3bf0</link>
      <guid>https://forem.com/aj_c6413caf1a793de3a2163b/laravel-local-scopes-3bf0</guid>
      <description>&lt;h2&gt;
  
  
  1. Introduction to Laravel Scopes
&lt;/h2&gt;

&lt;p&gt;Laravel Scope is one of the best features of Laravel, it is a querying method in which we can create a scope in a model or a new class. It is a powerful tool to help you write reusable and maintainable code.&lt;/p&gt;

&lt;h3&gt;
  
  
  How To Integrate Scopes.
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$activeUser = Post::where('active', true)-&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But I want to get Active User, Again and Again, So right now I need to write this query again and again and it will show duplication and not a standard way. So Here is the Solution in Scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User extends Model
{    
    public function scopeActive($query)
    {
        return $query-&amp;gt;where('active', 1);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In This picture.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User is a model class that encapsulates the model data.&lt;/li&gt;
&lt;li&gt;It has a method called scopeActive.&lt;/li&gt;
&lt;li&gt;ScopeActive adds a filter to the database Query and ensures that only active users are returned from a database.&lt;/li&gt;
&lt;li&gt;It is recommended that the scope method name be started with a lowercase letter.&lt;/li&gt;
&lt;li&gt;Now we can use this function to filter out data related to the user in a controller.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$activeUser = User::active()-&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How To Use Dynamic Scopes.&lt;/strong&gt;&lt;br&gt;
If we want to use Scope Dynamically, we need to pass the parameter for this scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User extends Model
{
  public function scopeActive($query,$value)
    {
        return $query-&amp;gt;where('active', $value);                
        //there is another way also to call this 
        //return $query-&amp;gt;whereActive($value);    
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can use dynamic Scope just like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$activeUser = User::active(true)-&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How To Use Scopes with Relations.
&lt;/h3&gt;

&lt;p&gt;A scope can also be defined in the relation model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User extends Model
{
  public function scopePosts($query,$value)
    {
        return $query-&amp;gt;with('posts');                
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can then use this scope to retrieve all posts for a specific user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$user = User::find(1);

$posts = $user-&amp;gt;posts()-&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How To Use Scopes with Joins.
&lt;/h3&gt;

&lt;p&gt;We can use Scope with joins also.. For example, if you have a model called Order that belongs to a User, and you want to retrieve all orders for a user that have a total greater than $100, you can use a scope that joins the Order model and applies a where clause to the resulting query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function scopeTotalGreaterThan($query, $amount) {

    return $query-&amp;gt;join('orders', 'users.id', '=', 'orders.user_id')

    -&amp;gt;where('orders.total', '&amp;gt;', $amount);

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

&lt;/div&gt;



&lt;p&gt;You can then use this scope to retrieve all users with orders that have a total greater than $100:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$users = User::totalGreaterThan(100)-&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Best Practices for Implementing Laravel Scopes
&lt;/h2&gt;

&lt;p&gt;6 Best Practices for Implementing Laravel Scopes&lt;br&gt;
Implementing Laravel scopes requires attention to detail and best practices to ensure that the code runs smoothly and effectively. Here are six best practices for implementing Laravel scopes in your projects:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose the Right Scope Name - The scope name should reflect the exact behavior of the query. A descriptive and concise name makes it easier to understand the purpose of the scope.&lt;/li&gt;
&lt;li&gt;Use Scopes Sparingly - Too many scopes can make your code hard to maintain. Consider using scopes only when you expect to reuse the code repeatedly.&lt;/li&gt;
&lt;li&gt;Keep it Atomic - Each scope should contain only one specific query constraint. This helps to maintain the clarity and readability of your code.&lt;/li&gt;
&lt;li&gt;Use Query Builder Constraints - Laravel scopes can use all the query builder constraints available in Laravel. This includes where clauses, joins, and limits.&lt;/li&gt;
&lt;li&gt;Use Local Scopes - Local scopes are defined inside a model and can be used within the model for filtering or querying. This helps to maintain clean and concise code.&lt;/li&gt;
&lt;li&gt;Test Your Scopes - Testing is important to ensure that your code works as expected. Create unit tests to verify that the scope is filtering the query correctly.
By following these practices, you can implement Laravel scopes effectively and efficiently in your projects. Laravel scopes are a powerful tool that can help you achieve consistent, secure, and reusable database queries in your Laravel applications.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;Thanks&lt;br&gt;
And if you love the content and want to support more awesome articles, consider buying me a coffee! ☕️🥳 Your support means the world to me and helps keep the knowledge flowing. You can do that right here: 👉 &lt;a href="https://buymeacoffee.com/jainaman" rel="noopener noreferrer"&gt;Buy Me a Coffee&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbpwc3kkwwzvglzzp6pcs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbpwc3kkwwzvglzzp6pcs.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>php</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Global Scope in Laravel (StepWise).</title>
      <dc:creator>AJ</dc:creator>
      <pubDate>Mon, 23 Sep 2024 15:49:36 +0000</pubDate>
      <link>https://forem.com/aj_c6413caf1a793de3a2163b/global-scope-in-laravel-stepwise-1ij8</link>
      <guid>https://forem.com/aj_c6413caf1a793de3a2163b/global-scope-in-laravel-stepwise-1ij8</guid>
      <description>&lt;p&gt;&lt;code&gt;Global Scopes are a vital concept in Laravel, enabling the reuse of Eloquent conditions throughout your application. By implementing Global Scopes, you can apply specific conditions to queries across all models, promoting code reuse and consistency. In contrast, Local Scopes are limited to a single model. In this tutorial, we will focus on creating and utilizing Global Scopes in Laravel.&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In This Step, we will create a Global Class inside a
&lt;code&gt;app/Scopes/ActiveScope&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

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

namespace app\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class ActiveScope implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        // Define your global condition here
        $builder-&amp;gt;where('is_active', '=', '1');

        //or we can write
        $builder-&amp;gt;whereIsActive('1');

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Now Define ActiveScope in the User Model. We Should override a given model’s boot method and use the addGlobalScope method:
&lt;/li&gt;
&lt;/ol&gt;

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

namespace App;

use App\Scopes\AgeScope;
use Illuminate\Database\Eloquent\Model;
use App\Scopes\ActiveScope;

class User extends Model
{
    /**
     * The "booting" method of the model.
     *
     * @return void
     */
    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope(new ActiveScope);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After adding the ActiveScope in model, User::all() will generate the following SQL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select * from `users` where `is_active` = '1'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There may be scenarios where you want to fetch all data without applying the global scope. In Laravel, you can bypass a global scope and fetch all data by using the withoutGlobalScope method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User::withoutGlobalScope(ActiveScope::class)-&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to remove multiple or all of the global scopes applied to a model, you can use the withoutGlobalScopes method in Laravel. This method allows you to bypass all global scopes or specify the ones you want to remove. Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Remove all of the global scopes...
User::withoutGlobalScopes()-&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Remove some of the global scopes...
User::withoutGlobalScopes([
    ActiveScope::class, AgeScope::class
])-&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;And if you love the content and want to support more awesome articles, consider buying me a coffee! ☕️🥳 Your support means the world to me and helps keep the knowledge flowing. You can do that right here: 👉 &lt;a href="https://buymeacoffee.com/jainaman" rel="noopener noreferrer"&gt;Buy Me a Coffee&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>softwaredevelopment</category>
      <category>coding</category>
    </item>
  </channel>
</rss>
