<?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: DaleLanto</title>
    <description>The latest articles on Forem by DaleLanto (@dalelantowork).</description>
    <link>https://forem.com/dalelantowork</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%2F810164%2F65e254c1-6d11-48d8-a2c0-b462b5af0a89.jpg</url>
      <title>Forem: DaleLanto</title>
      <link>https://forem.com/dalelantowork</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dalelantowork"/>
    <language>en</language>
    <item>
      <title>Laravel 8 - Accessors and Mutators (Eloquent in 2 Steps)</title>
      <dc:creator>DaleLanto</dc:creator>
      <pubDate>Fri, 04 Mar 2022 09:14:09 +0000</pubDate>
      <link>https://forem.com/dalelantowork/laravel-8-accessors-and-mutators-4d8m</link>
      <guid>https://forem.com/dalelantowork/laravel-8-accessors-and-mutators-4d8m</guid>
      <description>&lt;p&gt;Laravel &lt;strong&gt;Accessors&lt;/strong&gt; and &lt;strong&gt;Mutators&lt;/strong&gt; are custom, user defined methods. &lt;strong&gt;Accessors&lt;/strong&gt; are used to format the attributes when you retrieve them from database. Whereas, &lt;strong&gt;Mutators&lt;/strong&gt; are used to format the attributes before saving them into the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lets' Start Creating and Using Accessor and Mutators!
&lt;/h2&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%2Fby3xxveez01cib7i7qrz.gif" 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%2Fby3xxveez01cib7i7qrz.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Suppose that you have a &lt;code&gt;Model&lt;/code&gt; that looks like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/Product.php&lt;/strong&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 App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasFactory;

    protected $fillable = [
        'name', 'amount', 'description'
    ];

    // Mutator for Name column
    // when "name" will save, it will convert into lowercase
    public function setNameAttribute($value)
    {
        $this-&amp;gt;attributes['name'] = strtolower($value);
    }
}


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

&lt;/div&gt;

&lt;p&gt;and a &lt;code&gt;Controller&lt;/code&gt; that looks like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ProductController.php&lt;/strong&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 App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;

class ProductController extends Controller
{
    public function setProduct()
    {
        $product = new Product();

        $product-&amp;gt;name = "Sample Product 1";
        $product-&amp;gt;amount = 12;
        $product-&amp;gt;description = "Sample product created";

        $product-&amp;gt;save();
    }
}


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

&lt;/div&gt;

&lt;p&gt;also a &lt;code&gt;route&lt;/code&gt; that looks like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;web.php&lt;/strong&gt;&lt;/p&gt;

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

Route::get('product', [ProductController::class, 'setProduct']);


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 1: Create Mutators In The Model
&lt;/h2&gt;

&lt;p&gt;A mutator transforms an Eloquent attribute value when it is set. Mutators work when we save data inside database table.&lt;/p&gt;

&lt;p&gt;Syntax to create mutators –&lt;/p&gt;

&lt;p&gt;To define a mutator, define a set{Attribute}Attribute method on your model where {Attribute} is the “studly” cased name of the column which we want to get altered when saved.&lt;/p&gt;

&lt;p&gt;Paste this in &lt;strong&gt;app/Product.php&lt;/strong&gt; &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

public function setNameAttribute($value)
    {
        $this-&amp;gt;attributes['name'] = strtolower($value);
    }


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

&lt;/div&gt;

&lt;p&gt;It should now look like this:&lt;/p&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\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasFactory;

    protected $fillable = [
        'name', 'amount', 'description'
    ];

    // Mutator for Name column
    // when "name" will save, it will convert into lowercase
    public function setNameAttribute($value)
    {
        $this-&amp;gt;attributes['name'] = strtolower($value);
    }
}


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Check the result
&lt;/h2&gt;

&lt;p&gt;Go to URL: &lt;a href="http://127.0.0.1/product" rel="noopener noreferrer"&gt;http://127.0.0.1/product&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result should look something like this:&lt;br&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%2Fxn9bt8r4a2h6grnkw5za.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%2Fxn9bt8r4a2h6grnkw5za.png" alt="Image description"&gt;&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%2Fjibci6g8d4cwv1ysla4k.gif" 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%2Fjibci6g8d4cwv1ysla4k.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: Create Accessors In The Model
&lt;/h2&gt;

&lt;p&gt;An accessor transforms an Eloquent attribute value when it is accessed. It works when we fetch data from table.&lt;/p&gt;

&lt;p&gt;Syntax to create accessor –&lt;/p&gt;

&lt;p&gt;To define an accessor, create a get{Attribute}Attribute method on your model where {Attribute} is the “studly” cased name of the column.&lt;/p&gt;

&lt;p&gt;Open &lt;code&gt;Product.php&lt;/code&gt; and add this code into it&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

public function getNameAttribute($value)
    {
        return strtoupper($value);
    }


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

&lt;/div&gt;

&lt;p&gt;Open &lt;code&gt;ProductController.php&lt;/code&gt; controller file and add this method into it.&lt;/p&gt;

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

public function getProducts()
    {
        $products = Product::get();

        foreach ($products as $product) {

            echo $product-&amp;gt;name . "&amp;lt;br/&amp;gt;";
        }
    }


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

&lt;/div&gt;

&lt;p&gt;Open &lt;code&gt;web.php&lt;/code&gt; and add this route into it.&lt;/p&gt;


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

&lt;p&gt;Route::get('list-product', [ProductController::class, 'getProducts']);&lt;/p&gt;

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

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Check the Result&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;URL: &lt;a href="http://127.0.0.1/list-product" rel="noopener noreferrer"&gt;http://127.0.0.1/list-product&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You should see the list of names (name of product) each in uppercase. This is because we have altered value by accessor method.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hurray! We have successfully created and used &lt;strong&gt;accessors&lt;/strong&gt; and &lt;strong&gt;mutators&lt;/strong&gt; , use this on your future projects to improve the readability , stability and performance of your code!&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%2Fr5pdqm5om0je2gv2eobd.gif" 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%2Fr5pdqm5om0je2gv2eobd.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>beginners</category>
      <category>bestpractices</category>
    </item>
    <item>
      <title>Laravel 8 - Scopes (3 Easy Steps)</title>
      <dc:creator>DaleLanto</dc:creator>
      <pubDate>Fri, 04 Mar 2022 06:35:17 +0000</pubDate>
      <link>https://forem.com/dalelantowork/laravel-8-scopes-26k3</link>
      <guid>https://forem.com/dalelantowork/laravel-8-scopes-26k3</guid>
      <description>&lt;p&gt;&lt;strong&gt;Scoping&lt;/strong&gt; is one of the superpowers that eloquent grants to developers when querying a model. &lt;strong&gt;Scopes&lt;/strong&gt; allow developers to add &lt;code&gt;constraints&lt;/code&gt; to queries for a given model.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In simple terms &lt;strong&gt;laravel scope&lt;/strong&gt; is just a query, a &lt;code&gt;query&lt;/code&gt; to make the code &lt;code&gt;shorter&lt;/code&gt; and &lt;code&gt;faster&lt;/code&gt;. We can create custom query with relation or anything with scopes. &lt;/p&gt;

&lt;p&gt;In any admin project we need to get data using status base, just like active, pending, draft something else. Instead of writing the same query everytime just use scopes and you're done!&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%2Fm2u7wbmc1ge45wmalsyp.gif" 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%2Fm2u7wbmc1ge45wmalsyp.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's Start Creating our Scope!
&lt;/h2&gt;

&lt;p&gt;Let's create a simple with today() scope and it will get only today records.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Create Scope in Model
&lt;/h2&gt;

&lt;p&gt;We will add today scope in our post model. So when we query in controller then we will use that scope in laravel model.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/Post.php&lt;/strong&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 App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public $table = "posts";

    protected $fillable = [
        'id', 'title', 'body', 'status'
    ];

    public function scopeToday($query)
    {
        return $query-&amp;gt;whereDate('created_at', \Carbon\Carbon::today());
    }
}


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 2: Use the scope in your controller
&lt;/h2&gt;

&lt;p&gt;Go to you controller and use the scope by pasting this:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Post::today()-&amp;gt;get();


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

&lt;/div&gt;

&lt;p&gt;We can also include parameters to make the scope more dynamic!&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Make the Scope Dynamic by Adding Parameters
&lt;/h2&gt;

&lt;p&gt;Lets go back to the model and add parameters to the scope &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/Post.php&lt;/strong&gt;&lt;/p&gt;

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

/**
     * Scope created awhile ago
     */
public function scopeToday($query)
    {
        return $query-&amp;gt;whereDate('created_at', \Carbon\Carbon::today());
    }
/**
     * New Dynamic Scope
     */
public function scopeStatus($query, $type)
    {
        return $query-&amp;gt;where('status', $type);
    }


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Use Both in your controller!
&lt;/h2&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Post::status(1)-&amp;gt;today()-&amp;gt;get();


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

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Hurray! We have succesfully created our scopes (local scopes) and used it, now use it to your projects and make your code more readable and stable!&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%2Fwjkomzmcf943l79u92e2.gif" 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%2Fwjkomzmcf943l79u92e2.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I don't recommend the usage of Global Scopes because it can mess up the readability of queries when you are working with a team, specially if there are new members of the team. &lt;/p&gt;

&lt;p&gt;But if you want to use it click &lt;a href="https://www.itsolutionstuff.com/post/laravel-global-scope-tutorial-exampleexample.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instead just use Local Scopes which we discussed here and you are good to go!&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%2Fjowiq4q986im59flg966.gif" 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%2Fjowiq4q986im59flg966.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>beginners</category>
      <category>bestpractices</category>
    </item>
    <item>
      <title>Laravel 8 - Traits (3 Easy Steps)</title>
      <dc:creator>DaleLanto</dc:creator>
      <pubDate>Fri, 04 Mar 2022 03:57:27 +0000</pubDate>
      <link>https://forem.com/dalelantowork/laravel-8-traits-4ai</link>
      <guid>https://forem.com/dalelantowork/laravel-8-traits-4ai</guid>
      <description>&lt;p&gt;Using &lt;code&gt;traits&lt;/code&gt; is one of the best practices alongside OOP(Object-Oriented Programming) and [SOLID Principles] in PHP.(&lt;a href="https://dev.to/dalelantowork/solid-principles-object-oriented-programming-in-php-3p3e"&gt;https://dev.to/dalelantowork/solid-principles-object-oriented-programming-in-php-3p3e&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a Trait?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Traits&lt;/strong&gt; are a mechanism for code reuse in single inheritance languages such as PHP. &lt;/p&gt;

&lt;p&gt;A trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In simple terms &lt;strong&gt;Traits&lt;/strong&gt; is a &lt;strong&gt;group of methods&lt;/strong&gt; that you want to &lt;strong&gt;include&lt;/strong&gt; within another class. You can easily &lt;strong&gt;reuse&lt;/strong&gt; that methods to another class. Trait is save to write same code again and again.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Let's Start Creating Our Own Custom Trait!
&lt;/h2&gt;

&lt;p&gt;We will create one trait &lt;code&gt;ImageTrait&lt;/code&gt; and in that trait we will write code for image upload. &lt;/p&gt;

&lt;p&gt;Whenever we need to upload image then we can use this ImageTrait trait.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Create Traits file and ImageTrait
&lt;/h2&gt;

&lt;p&gt;Create a folder inside &lt;code&gt;app&lt;/code&gt; directory named &lt;code&gt;Traits&lt;/code&gt; and inside it create &lt;code&gt;ImageTrait&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's create a new trait with &lt;code&gt;verifyAndUpload()&lt;/code&gt; function. Paste the code below:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/Traits/ImageTrait.php&lt;/strong&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 App\Traits;

use Illuminate\Http\Request;

trait ImageTrait {

    /**
     * @param Request $request
     * @return $this|false|string
     */
    public function verifyAndUpload(Request $request, $fieldname = 'image', $directory = 'images' ) {

        if( $request-&amp;gt;hasFile( $fieldname ) ) {

            if (!$request-&amp;gt;file($fieldname)-&amp;gt;isValid()) {

                flash('Invalid Image!')-&amp;gt;error()-&amp;gt;important();

                return redirect()-&amp;gt;back()-&amp;gt;withInput();

            }

            return $request-&amp;gt;file($fieldname)-&amp;gt;store($directory, 'public');

        }

        return null;

    }

}


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 2: Create the controller that will use ImageTrait
&lt;/h2&gt;

&lt;p&gt;Paste the code below.&lt;/p&gt;

&lt;p&gt;app/Http/Controllers/ItemController.php&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Item;

class ItemController extends Controller
{
     /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('imageUpload');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = $request-&amp;gt;all();

        $input['image'] = '';

        Item::create($input);

        return back()
            -&amp;gt;with('success','record created successfully.');

    }
}


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 3: Insert and use ImageTrait on your Controller
&lt;/h2&gt;

&lt;p&gt;Paste the uses of ImageTrait.&lt;br&gt;
&lt;strong&gt;app/Http/Controllers/ItemController.php&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

use App\Traits\ImageTrait;


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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;at the top part&lt;/li&gt;
&lt;/ul&gt;

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

use ImageTrait;


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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;inside the controller class&lt;/li&gt;
&lt;/ul&gt;

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

$input['image'] = $this-&amp;gt;verifyAndUpload($request, 'image', 'images');


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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;using the function inside the trait&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Now it should like this!&lt;/strong&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 App\Http\Controllers;

use Illuminate\Http\Request;
use App\Item;
use App\Traits\ImageTrait;

class ItemController extends Controller
{
    use ImageTrait;
     /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('imageUpload');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = $request-&amp;gt;all();

        $input['image'] = $this-&amp;gt;verifyAndUpload($request, 'image', 'images');

        Item::create($input);

        return back()
            -&amp;gt;with('success','record created successfully.');

    }
}



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

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Hurray we have successfully created and applied Traits! You can now use this in your projects and improve the readability and stability of your code!&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%2Fvm1u9zx6skesg9h8poe6.gif" 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%2Fvm1u9zx6skesg9h8poe6.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>beginners</category>
      <category>bestpractices</category>
    </item>
    <item>
      <title>Laravel 8 - SweetAlert2 with AJAX (3 Easy Steps)🍬🍭🧁</title>
      <dc:creator>DaleLanto</dc:creator>
      <pubDate>Sat, 26 Feb 2022 05:04:44 +0000</pubDate>
      <link>https://forem.com/dalelantowork/laravel-8-sweetalert2-with-ajax-for-beginners-3-easy-steps-1p6d</link>
      <guid>https://forem.com/dalelantowork/laravel-8-sweetalert2-with-ajax-for-beginners-3-easy-steps-1p6d</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is SweetAlert2?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;SweetAlert2 is a beautiful, customizable, accessible replacement for javascript's pop-up boxes with zero dependencies!&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%2Fhjzjkdbe4a1s3s6kfkbk.gif" 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%2Fhjzjkdbe4a1s3s6kfkbk.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Read more about &lt;a href="https://sweetalert2.github.io/" rel="noopener noreferrer"&gt;sweetalert2&lt;/a&gt; here.&lt;/p&gt;

&lt;p&gt;Now that you have a good understanding of &lt;code&gt;sweetalert2&lt;/code&gt;, lets start!&lt;/p&gt;

&lt;h2&gt;
  
  
  STEP 1: Create Controller
&lt;/h2&gt;

&lt;p&gt;Create user controller using this command:&lt;/p&gt;

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

php artisan make:controller UserController


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

&lt;/div&gt;

&lt;p&gt;Open user controller and paste this code:&lt;/p&gt;

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

&amp;lt;?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    // all users
    public function users()
    {
        $users = User::all();
        return view('users', compact('users'));
    }

    // delete user
    public function delete($id)
    {
        $delete = User::destroy($id);

        // check data deleted or not
        if ($delete == 1) {
            $success = true;
            $message = "User deleted successfully";
        } else {
            $success = true;
            $message = "User not found";
        }

        //  return response
        return response()-&amp;gt;json([
            'success' =&amp;gt; $success,
            'message' =&amp;gt; $message,
        ]);
    }
}


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

&lt;/div&gt;

&lt;p&gt;Here we’ve created two functions. One for getting all users and another for deleting user.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Define Routes &amp;amp; Insert Fake data
&lt;/h2&gt;

&lt;p&gt;Let’s define routes for those two functions:&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\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

Route::get('users', [UserController::class, 'users']);
Route::post('delete/{id}', [UserController::class, 'delete']);


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

&lt;/div&gt;

&lt;p&gt;Now let's insert some fake user data using factory and seeder:&lt;/p&gt;

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

\App\Models\User::factory(5)-&amp;gt;create(); // 5 fake user data


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

&lt;/div&gt;

&lt;p&gt;Read more about &lt;a href="https://dev.to/dalelantowork/laravel-8-factory-and-seeders-generating-thousands-of-dummy-data-instantly-4mi2"&gt;factory and seeder&lt;/a&gt; here.&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%2F9l0viz4uya0q56eoqx61.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%2F9l0viz4uya0q56eoqx61.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Set SweetAlert2 with AJAX in the Blade File
&lt;/h2&gt;

&lt;p&gt;Create a view named &lt;code&gt;users.blade.php&lt;/code&gt; and paste this code:&lt;/p&gt;

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

&amp;lt;!doctype html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;!-- meta tags --&amp;gt;
    &amp;lt;meta charset="utf-8"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"&amp;gt;
    &amp;lt;!-- csrf-token --&amp;gt;
    &amp;lt;meta name="csrf-token" content="{{ csrf_token() }}"&amp;gt;
    &amp;lt;!-- Bootstrap CSS --&amp;gt;
    &amp;lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha2/css/bootstrap.min.css"&amp;gt;
    &amp;lt;!-- jquery --&amp;gt;
    &amp;lt;script src="https://code.jquery.com/jquery-3.5.1.min.js"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;!-- SweetAlert2 --&amp;gt;
    &amp;lt;link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/10.5.1/sweetalert2.min.css"&amp;gt;
    &amp;lt;script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/10.5.1/sweetalert2.all.min.js"&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;title&amp;gt;How to Use SweetAlert2 with AJAX in Laravel 8.x&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body class="container" style="margin-top: 40px;"&amp;gt;

&amp;lt;div class="row" style="margin-bottom: 20px;"&amp;gt;
    &amp;lt;div class="col-lg-12 margin-tb"&amp;gt;
        &amp;lt;div class="pull-left"&amp;gt;
            &amp;lt;h3&amp;gt;Users&amp;lt;/h3&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;

&amp;lt;table class="table table-bordered"&amp;gt;
    &amp;lt;tr&amp;gt;
        &amp;lt;th&amp;gt;ID&amp;lt;/th&amp;gt;
        &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;
        &amp;lt;th&amp;gt;Email&amp;lt;/th&amp;gt;
        &amp;lt;th width="280px"&amp;gt;Actions&amp;lt;/th&amp;gt;
    &amp;lt;/tr&amp;gt;
    @foreach ($users as $user)
        &amp;lt;tr&amp;gt;
            &amp;lt;td&amp;gt;{{ $user-&amp;gt;id }}&amp;lt;/td&amp;gt;
            &amp;lt;td&amp;gt;{{ $user-&amp;gt;name }}&amp;lt;/td&amp;gt;
            &amp;lt;td&amp;gt;{{ $user-&amp;gt;email }}&amp;lt;/td&amp;gt;
            &amp;lt;td&amp;gt;
                &amp;lt;button class="btn btn-danger" onclick="deleteConfirmation({{$user-&amp;gt;id}})"&amp;gt;Delete&amp;lt;/button&amp;gt;
            &amp;lt;/td&amp;gt;
        &amp;lt;/tr&amp;gt;
    @endforeach
&amp;lt;/table&amp;gt;

&amp;lt;script type="text/javascript"&amp;gt;
    function deleteConfirmation(id) {
        swal.fire({
            title: "Delete?",
            icon: 'question',
            text: "Please ensure and then confirm!",
            type: "warning",
            showCancelButton: !0,
            confirmButtonText: "Yes, delete it!",
            cancelButtonText: "No, cancel!",
            reverseButtons: !0
        }).then(function (e) {

            if (e.value === true) {
                var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');

                $.ajax({
                    type: 'POST',
                    url: "{{url('/delete')}}/" + id,
                    data: {_token: CSRF_TOKEN},
                    dataType: 'JSON',
                    success: function (results) {
                        if (results.success === true) {
                            swal.fire("Done!", results.message, "success");
                            // refresh page after 2 seconds
                            setTimeout(function(){
                                location.reload();
                            },2000);
                        } else {
                            swal.fire("Error!", results.message, "error");
                        }
                    }
                });

            } else {
                e.dismiss;
            }

        }, function (dismiss) {
            return false;
        })
    }
&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;Now that we've finished setting everything up, let's check it out!&lt;/p&gt;

&lt;h2&gt;
  
  
  Run and See Output
&lt;/h2&gt;

&lt;p&gt;Run the Laravel project &lt;/p&gt;

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

php artisan serve


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

&lt;/div&gt;

&lt;p&gt;Visit this route &lt;code&gt;http://localhost:8000/users&lt;/code&gt; and you will see a page like this:&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%2F4w0jt0ji1o2fhmjcihza.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%2F4w0jt0ji1o2fhmjcihza.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click &lt;strong&gt;Delete&lt;/strong&gt; button and the SweetAlert2 confirmation box will appear:&lt;br&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%2F2mu76fpm4kl1euoim2av.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%2F2mu76fpm4kl1euoim2av.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click &lt;strong&gt;Yes&lt;/strong&gt; button, the data will be deleted via AJAX and you will see a confirmation message like this:&lt;br&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%2Fb3jqo7em5drmfbhm05co.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%2Fb3jqo7em5drmfbhm05co.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hurray! We have succesfully created and tested &lt;code&gt;sweetalert2&lt;/code&gt;, use this on your future projects to make it more beautiful! &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%2Fdpuotv698az8qh7ym0qs.gif" 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%2Fdpuotv698az8qh7ym0qs.gif" alt="Image description"&gt;&lt;/a&gt; &lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>ajax</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Laravel 8 - DataTables Server Side Rendering (5 easy steps) 💾📈📊</title>
      <dc:creator>DaleLanto</dc:creator>
      <pubDate>Fri, 25 Feb 2022 12:07:49 +0000</pubDate>
      <link>https://forem.com/dalelantowork/laravel-8-datatables-server-side-rendering-5-easy-steps-5464</link>
      <guid>https://forem.com/dalelantowork/laravel-8-datatables-server-side-rendering-5-easy-steps-5464</guid>
      <description>&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%2Fvdadalgqnmvj6rbehbg0.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%2Fvdadalgqnmvj6rbehbg0.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DataTables&lt;/strong&gt; has two fundamental modes of operation:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Client-side processing&lt;/strong&gt; - where filtering, paging and sorting calculations are all performed in the web-browser.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server-side processing&lt;/strong&gt; - where filtering, paging and sorting calculations are all performed by a server.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does DataTables Server Side Rendering mean?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Imagine about the situation when you see &lt;strong&gt;thousands, hundreds of thousands or millions of records&lt;/strong&gt;, and you have to scan through every record to get the required information. &lt;/p&gt;

&lt;p&gt;Seems like difficult right? But not only that &lt;strong&gt;imagine the toll it would give to the browser&lt;/strong&gt;, you're website will literally blackout. &lt;/p&gt;

&lt;p&gt;Here comes &lt;strong&gt;Datatables&lt;/strong&gt; which makes our work less miserable and offers &lt;code&gt;quick search&lt;/code&gt;, &lt;code&gt;pagination&lt;/code&gt;, &lt;code&gt;ordering&lt;/code&gt;, &lt;code&gt;sorting&lt;/code&gt;functionalities to manage the data dynamically in the table.&lt;/p&gt;

&lt;p&gt;I will also give &lt;strong&gt;tips&lt;/strong&gt; on how &lt;strong&gt;to improve the performance and speed&lt;/strong&gt; of your Datatables at the end! &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%2Fh3ro5z78tgff2lkshmo6.gif" 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%2Fh3ro5z78tgff2lkshmo6.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets start creating our DataTables!&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Create the Laravel App and Install Yajra DataTables
&lt;/h2&gt;

&lt;p&gt;Run the below-mentioned artisan command to install a new laravel application.&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 laravel-yajra-datatables --prefer-dist


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

&lt;/div&gt;

&lt;p&gt;Then install the Yajra DataTable plugin in Laravel&lt;/p&gt;

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

composer require yajra/laravel-datatables-oracle


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

&lt;/div&gt;

&lt;p&gt;Go to &lt;code&gt;config/app.php&lt;/code&gt; file and paste this inside:&lt;/p&gt;

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

.....
.....
'providers' =&amp;gt; [
    ....
    ....
    Yajra\DataTables\DataTablesServiceProvider::class,
]
'aliases' =&amp;gt; [
    ....
    ....
    'DataTables' =&amp;gt; Yajra\DataTables\Facades\DataTables::class,
]
.....
.....


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

&lt;/div&gt;

&lt;p&gt;Run vendor publish command&lt;/p&gt;

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

php artisan vendor:publish --provider="Yajra\DataTables\DataTablesServiceProvider"


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

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Easy right? Now that setups is done let's create our files!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 2: Create Model and Migrations
&lt;/h2&gt;

&lt;p&gt;Run command to create a model&lt;/p&gt;

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

php artisan make:model Student -m


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

&lt;/div&gt;

&lt;p&gt;Open &lt;code&gt;database/migrations/timestamp_create_students_table.php&lt;/code&gt; file and paste&lt;/p&gt;

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

public function up()
{
    Schema::create('students', function (Blueprint $table) {
        $table-&amp;gt;id();
        $table-&amp;gt;string('name');
        $table-&amp;gt;string('email')-&amp;gt;unique();
        $table-&amp;gt;string('username');
        $table-&amp;gt;string('phone');
        $table-&amp;gt;string('dob');
        $table-&amp;gt;timestamps();
    });
}


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

&lt;/div&gt;

&lt;p&gt;Open &lt;code&gt;app/Models/Student.php&lt;/code&gt; and paste in the &lt;code&gt;$fillable&lt;/code&gt; array.&lt;/p&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\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
    use HasFactory;
    protected $fillable = [
        'name',
        'email',
        'username',
        'phone',
        'dob',
    ];    
}


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

&lt;/div&gt;

&lt;p&gt;Run migration&lt;/p&gt;

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

php artisan migrate


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

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;We can't have a datatable without data right? So lets' create it!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Insert Dummy Data or Records&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Open the &lt;code&gt;database/seeds/DatabaseSeeder.php&lt;/code&gt; file and add the following code&lt;/p&gt;

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

&amp;lt;?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Faker\Factory as Faker;
class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $faker = Faker::create();
        $gender = $faker-&amp;gt;randomElement(['male', 'female']);
        foreach (range(1,200) as $index) {
            DB::table('students')-&amp;gt;insert([
                'name' =&amp;gt; $faker-&amp;gt;name($gender),
                'email' =&amp;gt; $faker-&amp;gt;email,
                'username' =&amp;gt; $faker-&amp;gt;username,
                'phone' =&amp;gt; $faker-&amp;gt;phoneNumber,
                'dob' =&amp;gt; $faker-&amp;gt;date($format = 'Y-m-d', $max = 'now')
            ]);
        }
    }
}


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

&lt;/div&gt;

&lt;p&gt;Run the following command to generate dummy data:&lt;/p&gt;

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

php artisan db:seed


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

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Now you will have data that looks something like this!&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%2Fh5vnl8e19eiut4chhe1w.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%2Fh5vnl8e19eiut4chhe1w.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Easy right? 2 Steps all just for the setup! 3 Easy steps left for the actual datatable lol!&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%2F74zpbll7n3z4jxmoikzf.gif" 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%2F74zpbll7n3z4jxmoikzf.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Create DataTable Controller
&lt;/h2&gt;

&lt;p&gt;Create a controller using the below command&lt;/p&gt;

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

php artisan make:controller StudentController


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

&lt;/div&gt;

&lt;p&gt;Open &lt;code&gt;app/Http/Controllers/StudentController.php&lt;/code&gt; file and add the following code&lt;/p&gt;

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

&amp;lt;?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;
use DataTables;
class StudentController extends Controller
{
    public function index()
    {
        return view('welcome');
    }

    public function getStudents(Request $request)
    {
        if ($request-&amp;gt;ajax()) {
            $data = Student::latest()-&amp;gt;get();
            return Datatables::of($data)
                -&amp;gt;addIndexColumn()
                -&amp;gt;addColumn('action', function($row){
                    $actionBtn = '&amp;lt;a href="javascript:void(0)" class="edit btn btn-success btn-sm"&amp;gt;Edit&amp;lt;/a&amp;gt; &amp;lt;a href="javascript:void(0)" class="delete btn btn-danger btn-sm"&amp;gt;Delete&amp;lt;/a&amp;gt;';
                    return $actionBtn;
                })
                -&amp;gt;rawColumns(['action'])
                -&amp;gt;make(true);
        }
    }
}


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 4: Define Route
&lt;/h2&gt;

&lt;p&gt;Open routes/web.php and add the given code&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\StudentController;

Route::get('students', [StudentController::class, 'index']);
Route::get('students/list', [StudentController::class, 'getStudents'])-&amp;gt;name('students.list');


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 5: Create the View and Display Data inside the DataTable
&lt;/h2&gt;

&lt;p&gt;Open &lt;code&gt;resources/views/welcome.blade.php&lt;/code&gt; file and place the following code.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Laravel 8|7 Datatables Tutorial&amp;lt;/title&amp;gt;
    &amp;lt;meta name="csrf-token" content="{{ csrf_token() }}"&amp;gt;
    &amp;lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"/&amp;gt;
    &amp;lt;link href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css" rel="stylesheet"&amp;gt;
    &amp;lt;link href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap4.min.css" rel="stylesheet"&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;

&amp;lt;div class="container mt-5"&amp;gt;
    &amp;lt;h2 class="mb-4"&amp;gt;Laravel 7|8 Yajra Datatables Example&amp;lt;/h2&amp;gt;
    &amp;lt;table class="table table-bordered yajra-datatable"&amp;gt;
        &amp;lt;thead&amp;gt;
            &amp;lt;tr&amp;gt;
                &amp;lt;th&amp;gt;No&amp;lt;/th&amp;gt;
                &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;
                &amp;lt;th&amp;gt;Email&amp;lt;/th&amp;gt;
                &amp;lt;th&amp;gt;Username&amp;lt;/th&amp;gt;
                &amp;lt;th&amp;gt;Phone&amp;lt;/th&amp;gt;
                &amp;lt;th&amp;gt;DOB&amp;lt;/th&amp;gt;
                &amp;lt;th&amp;gt;Action&amp;lt;/th&amp;gt;
            &amp;lt;/tr&amp;gt;
        &amp;lt;/thead&amp;gt;
        &amp;lt;tbody&amp;gt;
        &amp;lt;/tbody&amp;gt;
    &amp;lt;/table&amp;gt;
&amp;lt;/div&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"&amp;gt;&amp;lt;/script&amp;gt;  
&amp;lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap4.min.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script type="text/javascript"&amp;gt;
  $(function () {

    var table = $('.yajra-datatable').DataTable({
        processing: true,
        serverSide: true,
        ajax: "{{ route('students.list') }}",
        columns: [
            {data: 'DT_RowIndex', name: 'DT_RowIndex'},
            {data: 'name', name: 'name'},
            {data: 'email', name: 'email'},
            {data: 'username', name: 'username'},
            {data: 'phone', name: 'phone'},
            {data: 'dob', name: 'dob'},
            {
                data: 'action', 
                name: 'action', 
                orderable: true, 
                searchable: true
            },
        ]
    });

  });
&amp;lt;/script&amp;gt;
&amp;lt;/html&amp;gt;


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

&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;DataTable()&lt;/code&gt; method is defined, and the &lt;code&gt;AJAX request&lt;/code&gt; is fetching the data from the server and displaying the &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;email&lt;/code&gt;, &lt;code&gt;user name&lt;/code&gt;, &lt;code&gt;phone number&lt;/code&gt; and &lt;code&gt;date of birth&lt;/code&gt; with the help of Yajra DataTable package.&lt;/p&gt;

&lt;p&gt;We have also set &lt;code&gt;orderable&lt;/code&gt; and &lt;code&gt;searchable&lt;/code&gt; properties to true, so that you can search the data smoothly and make your programming tasks prosperous.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Now lets check it! &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Run the following command and check our progress on the browser.&lt;/p&gt;

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

php artisan serve


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

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;This is what it should look like!&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%2F1ameo9r6gtnet3teuy3i.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%2F1ameo9r6gtnet3teuy3i.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hurray! We did it and in just 5 Easy and Quick Steps!&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%2F2iukb5hef5hr1ku4w2is.gif" 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%2F2iukb5hef5hr1ku4w2is.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Here are some related links to help you understand more about DataTables and Yajra:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=1wgLY-V69MM" rel="noopener noreferrer"&gt;Datatables in Laravel: Default and AJAX (Demo Project)&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You will see here the 3 different types of handling DataTables:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Laravel + HTML (Load Table on Page Load)&lt;/li&gt;
&lt;li&gt;Laravel + AJAX = DataTable (Client Side Rendering)&lt;/li&gt;
&lt;li&gt;Laravel + Yajra = DataTable (Server Side Rendering)&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://datatables.net/" rel="noopener noreferrer"&gt;DataTables&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://datatables.yajrabox.com/" rel="noopener noreferrer"&gt;Yajra Laravel DataTables Demo&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&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%2Ff6dh2kmoqlccnnitszmu.gif" 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%2Ff6dh2kmoqlccnnitszmu.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Tips on how to improve your DataTables!
&lt;/h2&gt;

&lt;p&gt;There are several ways in which you can speed up DataTables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip #1 Enable Paging&lt;/strong&gt;&lt;/p&gt;

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

$('#example').dataTable( {
    "paging": true
} );


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Tip #2 Disable OrderClasses&lt;/strong&gt;&lt;/p&gt;

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

$('#example').dataTable( {
  "orderClasses": false
} );


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Tip #3 Enable DeferRender&lt;/strong&gt;&lt;/p&gt;

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

$('#example').dataTable( {
  "ajax": "sources/arrays.txt",
  "deferRender": true
} );


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Tip #4 Enaber ServerSide&lt;/strong&gt;&lt;/p&gt;

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

$('#example').dataTable( {
  "serverSide": true,
  "ajax": "xhr.php"
} );


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Tip #5 Lighten the load on your query!&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Instead of getting all the columns and relationships on your query which can lead to a long time of loading, &lt;strong&gt;simplify your query&lt;/strong&gt; by getting only the necessary columns and data you need before passing it on the client-side.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can read more about it &lt;a href="https://datatables.net/faqs/index#speed" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>bestpractices</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Laravel 8 - Audit for Beginners (5 easy steps) ✍🏻📒💾</title>
      <dc:creator>DaleLanto</dc:creator>
      <pubDate>Thu, 24 Feb 2022 04:36:42 +0000</pubDate>
      <link>https://forem.com/dalelantowork/laravel-8-audit-for-beginners-5-easy-steps-4k6c</link>
      <guid>https://forem.com/dalelantowork/laravel-8-audit-for-beginners-5-easy-steps-4k6c</guid>
      <description>&lt;p&gt;&lt;strong&gt;Laravel Auditing&lt;/strong&gt; is a Laravel package that aims to make it easy to &lt;strong&gt;track eloquent model changes&lt;/strong&gt;. The documentation describes Laravel Auditing as follows:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Laravel Auditing allows you to keep a history of model changes by simply using a trait. Retrieving the audited data is straightforward, making it possible to display it in various ways.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Along with model changes, each audit record contains the &lt;code&gt;User Agent&lt;/code&gt;, &lt;code&gt;audit URL&lt;/code&gt;, and the &lt;code&gt;IP address&lt;/code&gt; of the user. One of the main use-cases of the package is looking at suspicious activities or unexpected changes in the model.&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%2Fvibufvlb5jhp7y1qfqx2.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%2Fvibufvlb5jhp7y1qfqx2.png" alt="Image description" width="800" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can read more about it &lt;a href="https://www.laravel-auditing.com/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now that you have an understanding of Laravel Auditing lets dive right in the codes!&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Install the package
&lt;/h2&gt;

&lt;p&gt;In the first step we have to install our package. So open your terminal and run this below command&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 owen-it/laravel-auditing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Generate and publish the vendor and the audits table migration by:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan vendor:publish --provider "OwenIt\Auditing\AuditingServiceProvider" --tag="migrations"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run Migrate:&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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Export the config file for the later adjustment
&lt;/h2&gt;



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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Apply in the Model
&lt;/h2&gt;

&lt;p&gt;Let's say I have a &lt;code&gt;Product&lt;/code&gt; model 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;namespace App\Models;

use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
    protected $guarded = [];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add update to your &lt;code&gt;Product&lt;/code&gt; model 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;namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Contracts\Auditable;
use OwenIt\Auditing\Auditable as AuditableTrait;

class Product extends Model implements Auditable
{
    use AuditableTrait;

    protected $guarded = [];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it. &lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Check the audits table in the database and you should see the auditing rows for the product.
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM `audits`

id user_type user_id event auditable_type auditable_id old_values new_values url ip_address user_agent tags created_at updated_at

1 App\Models\User 976 created App\Models\Product 1297 [] {"title":"Quisquam autem id ei","description":"Exc... http://laravel-prep.test/en/products 127.0.0.1 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWeb... NULL 2020-11-29 10:44:48 2020-11-29 10:44:48

2 App\Models\User 976 created App\Models\Product 1298 [] {"title":"Ullamco laboriosam","description":"Repel... http://laravel-prep.test/en/products 127.0.0.1 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWeb... NULL 2020-11-29 10:44:57 2020-11-29 10:44:57

3 App\Models\User 976 updated App\Models\Product 1296 {"title":"Quisquam autem id ei"} {"title":"Voluptatibus sunt i"} http://laravel-prep.test/en/products/1296 127.0.0.1 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWeb... NULL 2020-11-29 10:50:49 2020-11-29 10:50:49

4 App\Models\User 976 deleted App\Models\Product 1297 {"id":1297,"title":"Quisquam autem id ei","descrip... [] http://laravel-prep.test/en/products/1297 127.0.0.1 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWeb... NULL 2020-11-29 10:51:14 2020-11-29 10:51:14
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Hurray! we have now applied, run and tested Laravel Auditing in 5 easy steps. Hope this helped!&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%2Feoik8u9jqenjr5h5gc7r.gif" 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%2Feoik8u9jqenjr5h5gc7r.gif" alt="Image description" width="498" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To continue making use of &lt;code&gt;laravel-auditing&lt;/code&gt; in a more indepth guide and manner here is a related link:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=kxBRJvxx05Q" rel="noopener noreferrer"&gt;Laravel Auditing Package: Track all Your Model Changes&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>audit</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Laravel 8 - API Resources for Beginners (10 Steps) ⭐⭐⭐</title>
      <dc:creator>DaleLanto</dc:creator>
      <pubDate>Tue, 22 Feb 2022 03:07:37 +0000</pubDate>
      <link>https://forem.com/dalelantowork/laravel-8-api-resources-for-beginners-2cpa</link>
      <guid>https://forem.com/dalelantowork/laravel-8-api-resources-for-beginners-2cpa</guid>
      <description>&lt;p&gt;What does API Resources mean?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;API Resources&lt;/strong&gt; acts as a transformation layer that sits between our Eloquent models and the JSON responses that are actually returned by our API.&lt;br&gt;
&lt;strong&gt;API resources&lt;/strong&gt; present a way to easily transform our models into &lt;code&gt;JSON responses&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Sample commands for &lt;strong&gt;creating the resource class&lt;/strong&gt; and resource collection using php artisan &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;create a resource class&lt;/strong&gt;&lt;/p&gt;

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

$ php artisan make:resource UserResource


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;create a resource collection using either&lt;/strong&gt;&lt;/p&gt;

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

$ php artisan make:resource Users --collection
$ php artisan make:resource UserCollection


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

&lt;/div&gt;

&lt;p&gt;This is the representation of the database api we are gonna create.&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%2Fdpk5c3l243xo100ob326.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%2Fdpk5c3l243xo100ob326.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Lets start by creating a new laravel app
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;$ laravel new book-reviws-api&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  STEP 1: Create models and migrations
&lt;/h2&gt;

&lt;p&gt;The book reviews API will have three models: User, Book and Rating.&lt;/p&gt;

&lt;p&gt;We’ll start by creating the &lt;code&gt;Book&lt;/code&gt; model:&lt;br&gt;
&lt;code&gt;$ php artisan make:model Book -m&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, let’s open the migration file generated for the Book model and update the up() method as below:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;database/migrations/TIMESTAMP_create_books_table.php&lt;/strong&gt;&lt;/p&gt;

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

 public function up()
    {
      Schema::create('books', function (Blueprint $table) {
        $table-&amp;gt;increments('id');
        $table-&amp;gt;unsignedInteger('user_id');
        $table-&amp;gt;string('title');
        $table-&amp;gt;text('description');
        $table-&amp;gt;timestamps();
      });
    }


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

&lt;/div&gt;

&lt;p&gt;We’ll do the same for the &lt;code&gt;Rating&lt;/code&gt; model:&lt;br&gt;
&lt;code&gt;$ php artisan make:model Rating -m&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Open the migration file generated for the Rating model and update the up() method as below:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;database/migrations/TIMESTAMP_create_ratings_table.php&lt;/strong&gt;&lt;/p&gt;

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

public function up()
    {
      Schema::create('ratings', function (Blueprint $table) {
        $table-&amp;gt;increments('id');
        $table-&amp;gt;unsignedInteger('user_id');
        $table-&amp;gt;unsignedInteger('book_id');
        $table-&amp;gt;unsignedInteger('rating');
        $table-&amp;gt;timestamps();
      });
    }


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

&lt;/div&gt;

&lt;p&gt;Run the the command below to run the migrations:&lt;br&gt;
&lt;code&gt;$ php artisan migrate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Remember to enter your database details in the &lt;code&gt;.env&lt;/code&gt; file before running the command above.&lt;/p&gt;

&lt;h2&gt;
  
  
  STEP 2: Define relationships between models
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A user can add as many books as they wish, but a book can only belong to one user. So, the relationship between the User model and Book model is a &lt;code&gt;one-to-many&lt;/code&gt; relationship. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s define that. Add the code below inside the User model:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/User.php&lt;/strong&gt;&lt;/p&gt;

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

public function books()
    {
      return $this-&amp;gt;hasMany(Book::class);
    }


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

&lt;/div&gt;

&lt;p&gt;Next, let’s define the inverse relationship on the Book model:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/Book.php&lt;/strong&gt;&lt;/p&gt;

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

public function user()
    {
      return $this-&amp;gt;belongsTo(User::class);
    }


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

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Likewise, a book can be rated by various users, hence a book can have many ratings. A rating can only belong to one book. This is also a &lt;code&gt;one-to-many&lt;/code&gt; relationship. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Add the code below in the Book model:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/Book.php&lt;/strong&gt;&lt;/p&gt;

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

public function ratings()
    {
      return $this-&amp;gt;hasMany(Rating::class);
    }


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

&lt;/div&gt;

&lt;p&gt;Then we define the inverse relationship inside the Rating model:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/Rating.php&lt;/strong&gt;&lt;/p&gt;

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

public function book()
    {
      return $this-&amp;gt;belongsTo(Book::class);
    }


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  STEP 3: Allowing mass assignment on some fields
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;To avoid getting the mass assignment error which Laravel will throw by default, we need to specify the columns we want to be mass assigned.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s add the snippet below to our models respectively:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/Book.php&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

protected $fillable = ['user_id', 'title', 'description'];


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;app/Rating.php&lt;/strong&gt;&lt;/p&gt;

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

protected $fillable = ['book_id', 'user_id', 'rating'];


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  STEP 4: Defining API routes
&lt;/h2&gt;

&lt;p&gt;Let’s define our routes. Open &lt;code&gt;routes/api.php&lt;/code&gt; and add the line below to it:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  Route::apiResource('books', 'BookController');
    Route::post('books/{book}/ratings', 'RatingController@store');


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

&lt;/div&gt;

&lt;p&gt;Since we are building an API, we make use of &lt;code&gt;apiResource()&lt;/code&gt; to generate API only routes.&lt;/p&gt;

&lt;p&gt;Also, we define a route that will be used to rate a specified book. For instance, &lt;code&gt;/books/53/ratings&lt;/code&gt; will be used to rate the book with the ID of &lt;code&gt;53&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note:&lt;br&gt;
When building APIs with Laravel, it is recommended to use the &lt;code&gt;apiResource()&lt;/code&gt; method while defining resourceful routes, this will generate only API specific routes (&lt;code&gt;index&lt;/code&gt;, &lt;code&gt;store&lt;/code&gt;, &lt;code&gt;show&lt;/code&gt;, &lt;code&gt;update&lt;/code&gt; and &lt;code&gt;destroy&lt;/code&gt;). Unlike when you use the &lt;code&gt;resource()&lt;/code&gt; method, which will in addition to generating API specific routes, also generate &lt;code&gt;create&lt;/code&gt; and &lt;code&gt;edit&lt;/code&gt; routes, which aren’t needed when building an API.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  STEP 5: Creating the book resource
&lt;/h2&gt;

&lt;p&gt;Before we move on to create the&lt;code&gt;BooksController&lt;/code&gt;, let’s create a book resource class. We’ll make use of the artisan command &lt;code&gt;make:resource&lt;/code&gt; to generate a new book resource class. By default, resources will be placed in the &lt;code&gt;app/Http/Resources&lt;/code&gt; directory of our application.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ php artisan make:resource BookResource&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once that is created, let’s open it and update the &lt;code&gt;toArray()&lt;/code&gt; method as below:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/Http/Resources/BookResource.php&lt;/strong&gt;&lt;/p&gt;

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

 public function toArray($request)
    {
      return [
        'id' =&amp;gt; $this-&amp;gt;id,
        'title' =&amp;gt; $this-&amp;gt;title,
        'description' =&amp;gt; $this-&amp;gt;description,
        'created_at' =&amp;gt; (string) $this-&amp;gt;created_at,
        'updated_at' =&amp;gt; (string) $this-&amp;gt;updated_at,
        'user' =&amp;gt; $this-&amp;gt;user,
        'ratings' =&amp;gt; $this-&amp;gt;ratings,
      ];
    }


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

&lt;/div&gt;

&lt;p&gt;As the name suggests, this will transform the resource into an array.&lt;/p&gt;

&lt;p&gt;We can access the model properties directly from the &lt;code&gt;$this&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;Now we can make use of the &lt;code&gt;BookResource&lt;/code&gt; class in our controller.&lt;/p&gt;

&lt;h2&gt;
  
  
  STEP 6: Creating the book controller
&lt;/h2&gt;

&lt;p&gt;Let’s create the &lt;code&gt;BookController&lt;/code&gt;. For this, we’ll make use of the API controller generation feature &lt;br&gt;
&lt;code&gt;$ php artisan make:controller BookController --api&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, open it up and paste the following code into it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/Http/Controllers/BookController.php&lt;/strong&gt;&lt;/p&gt;

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

    use App\Book;
    use App\Http\Resources\BookResource;

// ...

    public function index()
    {
      return BookResource::collection(Book::with('ratings')-&amp;gt;paginate(25));
    }

    public function store(Request $request)
    {
      $book = Book::create([
        'user_id' =&amp;gt; $request-&amp;gt;user()-&amp;gt;id,
        'title' =&amp;gt; $request-&amp;gt;title,
        'description' =&amp;gt; $request-&amp;gt;description,
      ]);

      return new BookResource($book);
    }

    public function show(Book $book)
    {
      return new BookResource($book);
    }

    public function update(Request $request, Book $book)
    {
      // check if currently authenticated user is the owner of the book
      if ($request-&amp;gt;user()-&amp;gt;id !== $book-&amp;gt;user_id) {
        return response()-&amp;gt;json(['error' =&amp;gt; 'You can only edit your own books.'], 403);
      }

      $book-&amp;gt;update($request-&amp;gt;only(['title', 'description']));

      return new BookResource($book);
    }

    public function destroy(Book $book)
    {
      $book-&amp;gt;delete();

      return response()-&amp;gt;json(null, 204);
    }


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Functionalities:
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;index()&lt;/code&gt; method fetches and returns a list of the books that have been added. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;store()&lt;/code&gt; method creates a new book with the ID of the currently authenticated user along with the details of the book, and persists it to the database. Then we return a book resource based on the newly created book.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;show()&lt;/code&gt; method accepts a Book model (we are using route model binding here) and simply returns a book resource based on the specified book.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;update()&lt;/code&gt; method first checks to make sure the user trying to update a book is the owner of the book . If the user is not the owner of the book, we return an appropriate error message and set the HTTP status code to &lt;code&gt;403&lt;/code&gt;. Otherwise we update the book with the new details and return a book resource with the updated details.&lt;/p&gt;

&lt;p&gt;Lastly, the &lt;code&gt;destroy()&lt;/code&gt; method deletes a specified book from the database. Since the specified book has been deleted and no longer available, we set the HTTP status code of the response returned to 204.&lt;/p&gt;
&lt;h2&gt;
  
  
  STEP 7: Creating the rating resource
&lt;/h2&gt;

&lt;p&gt;Just as we did with the &lt;code&gt;BookResource&lt;/code&gt;, we’ll also create a rating resource class:&lt;br&gt;
&lt;code&gt;$ php artisan make:resource RatingResource&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once that is created, let’s open it and update the &lt;code&gt;toArray()&lt;/code&gt; method as below:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/Http/Resources/RatingResource.php&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

public function toArray($request)
    {
      return [
        'user_id' =&amp;gt; $this-&amp;gt;user_id,
        'book_id' =&amp;gt; $this-&amp;gt;book_id,
        'rating' =&amp;gt; $this-&amp;gt;rating,
        'created_at' =&amp;gt; (string) $this-&amp;gt;created_at,
        'updated_at' =&amp;gt; (string) $this-&amp;gt;updated_at,
        'book' =&amp;gt; $this-&amp;gt;book,
      ];
    }


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

&lt;/div&gt;

&lt;p&gt;Again, we pass along the attributes we want to be converted to JSON when sending the response. &lt;/p&gt;

&lt;h2&gt;
  
  
  STEP 8: Creating the rating controller
&lt;/h2&gt;

&lt;p&gt;Next, create the &lt;code&gt;RatingController&lt;/code&gt; that will make use of the &lt;code&gt;RatingResource&lt;/code&gt;:&lt;br&gt;
&lt;code&gt;$ php artisan make:controller RatingController&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, open it up and paste the following code into it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/Http/Controllers/RatingController.php&lt;/strong&gt;&lt;/p&gt;

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

 use App\Book;
    use App\Rating;
    use App\Http\Resources\RatingResource;

// ...

    public function store(Request $request, Book $book)
    {
      $rating = Rating::firstOrCreate(
        [
          'user_id' =&amp;gt; $request-&amp;gt;user()-&amp;gt;id,
          'book_id' =&amp;gt; $book-&amp;gt;id,
        ],
        ['rating' =&amp;gt; $request-&amp;gt;rating]
      );

      return new RatingResource($rating);
    }


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

&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;store()&lt;/code&gt; is used to rate a specified book. We are using the &lt;code&gt;firstOrCreate()&lt;/code&gt; which checks if a user has already rated a specified book.&lt;/p&gt;

&lt;p&gt;We add the user rating to the specified book and persist it to the database. Then we return a rating resource based on the newly added rating.&lt;/p&gt;

&lt;h2&gt;
  
  
  STEP 9: Getting average rating
&lt;/h2&gt;

&lt;p&gt;The last feature that’s left is getting the average rating made on a book&lt;/p&gt;

&lt;p&gt;Add the line of code below to the &lt;code&gt;toArray()&lt;/code&gt; method of &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/Http/Resources/BookResource.php&lt;/strong&gt;&lt;/p&gt;

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

'average_rating' =&amp;gt; $this-&amp;gt;ratings-&amp;gt;avg('rating')


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

&lt;/div&gt;

&lt;p&gt;We are using the &lt;code&gt;ratings&lt;/code&gt; relationship defined of the Book model to fetch all the ratings that have be made on the specified book. &lt;/p&gt;

&lt;p&gt;Then, using collection &lt;code&gt;avg()&lt;/code&gt;, we get the average of the ratings.&lt;/p&gt;

&lt;p&gt;Passing &lt;code&gt;rating&lt;/code&gt; to the &lt;code&gt;avg()&lt;/code&gt; function indicates that we want to calculate the average based on the book rating.&lt;/p&gt;

&lt;p&gt;Now, whenever the &lt;code&gt;BookResource&lt;/code&gt; is used, the response will contain the average rating of the book.&lt;/p&gt;

&lt;p&gt;Sample book resource response:&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%2Fhjyf3flcex1yzdvdhg16.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%2Fhjyf3flcex1yzdvdhg16.PNG" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sample rating resource response:&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%2Fljzplzx7nzhkae4m8jbn.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%2Fljzplzx7nzhkae4m8jbn.PNG" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  STEP 10: Handling resource not found
&lt;/h2&gt;

&lt;p&gt;By default when a specified model is not found, Laravel will throw a &lt;code&gt;ModelNotFoundException&lt;/code&gt; and renders a &lt;code&gt;404&lt;/code&gt; page. Since we are building an API, we want to handle the exception and throw an API friendly error message.&lt;/p&gt;

&lt;p&gt;Add the code below to the &lt;code&gt;render&lt;/code&gt; method of &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app/Exceptions/Handler.php&lt;/strong&gt;&lt;/p&gt;

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

if ($exception instanceof ModelNotFoundException &amp;amp;&amp;amp; $request-&amp;gt;wantsJson()) {
      return response()-&amp;gt;json([
        'error' =&amp;gt; 'Resource not found'
      ], 404);
    }


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

&lt;/div&gt;

&lt;p&gt;This checks if the exception thrown is an instance of &lt;code&gt;ModelNotFoundException&lt;/code&gt; and the request wants JSON, then we simply return a response with an error message of &lt;strong&gt;Resource not found&lt;/strong&gt; and set the HTTP status code to &lt;code&gt;404&lt;/code&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%2Ftr6pij65kmm3u8k5m0jf.gif" 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%2Ftr6pij65kmm3u8k5m0jf.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't forget! The API requests will need the header Accept: application/json in postman.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>api</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Laravel 8 - "Cruddy by Design" A Controller Structure for Beginners 🔥🔥🔥</title>
      <dc:creator>DaleLanto</dc:creator>
      <pubDate>Sun, 20 Feb 2022 13:56:01 +0000</pubDate>
      <link>https://forem.com/dalelantowork/laravel-8-cruddy-by-design-a-controller-structure-for-beginners-6d4</link>
      <guid>https://forem.com/dalelantowork/laravel-8-cruddy-by-design-a-controller-structure-for-beginners-6d4</guid>
      <description>&lt;p&gt;One of the easiest to understand Controller structures in Laravel is "&lt;strong&gt;Cruddy by Design&lt;/strong&gt;" by Adam Wathan.&lt;/p&gt;

&lt;p&gt;After the conference he published a new &lt;strong&gt;&lt;a href="https://github.com/adamwathan/laracon2017"&gt;GitHub repo&lt;/a&gt;&lt;/strong&gt; that contains the demo app he refactored on stage. The 4 main tips to improve your code come as &lt;strong&gt;&lt;a href="https://github.com/adamwathan/laracon2017/pull/1"&gt;PRs&lt;/a&gt;&lt;/strong&gt; on the repo with an full description on why the change is valuable. Very cool stuff.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=MF0jFKvS4SI&amp;amp;list=PLFj_ZOEtqR3paNWePc42LiJTmYGxFR4lw&amp;amp;index=1"&gt;Link&lt;/a&gt; of the video.&lt;/p&gt;

&lt;p&gt;I highly &lt;strong&gt;recommend this method specially for beginners&lt;/strong&gt; when you implement this and are working with a team, by looking at the logic of things in a CRUD way you can easily know where to find the code you need looking simply at the routes or controllers.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There's no real cost to having 10x as many controllers, given you can organise them and standardise naming. This approach has allowed several applications to remain easy to understand and manage as complexity grows; the http interface feels no more complex every time You add a new controller. If you were a developer introduced to a new codebase and you knew it used this approach, you would immediately know what "&lt;code&gt;PodcastTagsController&lt;/code&gt;" does and how to use it without even looking in it. You could open routes/web.php in my application and immediately understand the majority of how people interact with the application over http.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>beginners</category>
      <category>crud</category>
    </item>
    <item>
      <title>Git Workflow with Agile Jira for Beginners 📘📖🖥️</title>
      <dc:creator>DaleLanto</dc:creator>
      <pubDate>Fri, 18 Feb 2022 13:05:17 +0000</pubDate>
      <link>https://forem.com/dalelantowork/git-workflow-with-agile-jira-2ibe</link>
      <guid>https://forem.com/dalelantowork/git-workflow-with-agile-jira-2ibe</guid>
      <description>&lt;h2&gt;
  
  
  Agile Git Workflow with Jira
&lt;/h2&gt;

&lt;p&gt;This &lt;strong&gt;documentation&lt;/strong&gt; will show the git standard workflow for medium sized to big tech companies. &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%2Flcu9vbhoocgbkyfgp0sa.jpg" 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%2Flcu9vbhoocgbkyfgp0sa.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When working with a system that would be use by thousands of users every second it would be best to separate the backend and frontend system. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For example:&lt;br&gt;
PHP Laravel API - for the backend&lt;br&gt;
React JS - for the front end&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%2Fvpvgj9mm23an5dxd6yar.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%2Fvpvgj9mm23an5dxd6yar.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step by step guide when creating new branch based on your task
&lt;/h2&gt;

&lt;p&gt;After Cloning the specific repository, Branch out always to &lt;code&gt;master/main branch&lt;/code&gt; to avoid deprecated version.&lt;/p&gt;

&lt;p&gt;Sample Git workflow:&lt;/p&gt;

&lt;p&gt;Checkout master:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure master is up to date:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git pull
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Checkout feature branch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout feature/V1-1/create-user-roles
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Ftsxfemiodw7pclnvsfgx.jpg" 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%2Ftsxfemiodw7pclnvsfgx.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Branch name must based on the task you will develop, if it’s a &lt;strong&gt;Feature, Fix&lt;/strong&gt; or &lt;strong&gt;Bug&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Also add to the &lt;strong&gt;Jira ticket number&lt;/strong&gt; if available. &lt;br&gt;
You can use &lt;strong&gt;slash&lt;/strong&gt; for separator.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Example Branch name:&lt;br&gt;
feature/V1-1/create-user-roles&lt;br&gt;
Format:&lt;br&gt;
[Feature, HotFix, Bug]/JIRA Ticket Number/task description&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Git commit messages: Make useful and readable message when commit the branch. &lt;br&gt;
And also add Jira ticket when making messages.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Format:&lt;br&gt;
[Jira Ticket Number] [Feature, Bug, Fix] : commit messages&lt;br&gt;
Example:&lt;br&gt;
[V1-1] feature: adding user roles migratons&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is a very useful convention when integrating with Jira and Github.&lt;/p&gt;

&lt;p&gt;Push the new branch&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git push --set-upstream origin develop feature/V1-1/create-user-roles
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Merge/Pull request: Always request to the develop branch only, to avoid issues on the &lt;code&gt;master/develop&lt;/code&gt; Branch&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The tech lead or tech manager will be the one to merge and combine the pull requests.&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%2Fuom8b99v2oapz7xnj9lm.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%2Fuom8b99v2oapz7xnj9lm.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Always &lt;strong&gt;check carefully&lt;/strong&gt; your Jira ticket number when creating and commit branch.&lt;br&gt;
This will help the team to add changelog to Jira task from Github commit.&lt;/p&gt;

&lt;p&gt;When you are currently working on a task and need to update the &lt;code&gt;master&lt;/code&gt; branch without deleting your progress or messing up the codes you can do a &lt;code&gt;git stash&lt;/code&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%2Fpcvnwhu37fig0d8wdq8g.jpg" 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%2Fpcvnwhu37fig0d8wdq8g.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git stash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Update the master/main branch&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git pull origin master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bring your changes back&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git stash pop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Branch Tagging/Versioning
&lt;/h2&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%2F0fr3r5l34iezklsh7xtr.jpg" 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%2F0fr3r5l34iezklsh7xtr.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All task from the Sprint will be merged to the &lt;code&gt;develop&lt;/code&gt; branch once it’s already done from code review/checking&lt;/p&gt;

&lt;p&gt;Develop Branch must be compared to the &lt;code&gt;main/master&lt;/code&gt; Branch if there’s any conflicts and Behind/Ahead commits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Merge Develop branch to master branch&lt;/strong&gt; once it already clean from any conflicts.&lt;/p&gt;

&lt;p&gt;Make semantic number from the master branch to create a &lt;code&gt;release/tags&lt;/code&gt; versioning&lt;/p&gt;

&lt;p&gt;Sample Tags Versioning&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;userapi - V1 &lt;br&gt;
userchatapi - V2&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instructions&lt;/p&gt;

&lt;p&gt;Format: &lt;br&gt;
&lt;strong&gt;MAJOR.MINOR.PATCH&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MAJOR version when you make incompatible API changes,&lt;/li&gt;
&lt;li&gt;MINOR version when you add functionality in a backwards compatible manner, and&lt;/li&gt;
&lt;li&gt;PATCH version when you make backwards compatible bug fixes.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Example: &lt;br&gt;
v1.0.0&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Semantic versioning&lt;/strong&gt; is very useful for the deployment process when it comes to Production servers, You can easily &lt;code&gt;rollback&lt;/code&gt; specific version from the clean version when the installed version is buggy.&lt;/p&gt;

</description>
      <category>bestpractices</category>
      <category>git</category>
      <category>agile</category>
      <category>beginners</category>
    </item>
    <item>
      <title>PHP Laravel 8 - Best Naming Conventions for APIs ⭐⭐⭐⭐⭐</title>
      <dc:creator>DaleLanto</dc:creator>
      <pubDate>Fri, 18 Feb 2022 10:13:10 +0000</pubDate>
      <link>https://forem.com/dalelantowork/php-laravel-8-best-naming-conventions-for-apis-23i0</link>
      <guid>https://forem.com/dalelantowork/php-laravel-8-best-naming-conventions-for-apis-23i0</guid>
      <description>&lt;p&gt;This here are the basic naming conventions which are followed by most big tech companies that uses PHP.&lt;/p&gt;

&lt;p&gt;This naming conventions is in compliance with Laravel [PHP] coding Standards with &lt;a href="https://www.php-fig.org/psr/" rel="noopener noreferrer"&gt;PSR&lt;/a&gt; specifications.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Best combined with &lt;a href="https://dev.to/dalelantowork/solid-principles-object-oriented-programming-in-php-3p3e"&gt;SOLID&lt;/a&gt; Principles&lt;/p&gt;

&lt;p&gt;And remember to follow the &lt;a href="https://en.wikipedia.org/wiki/KISS_principle" rel="noopener noreferrer"&gt;KISS&lt;/a&gt; Principle whenever possible&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%2Fr8z23h1kcspsxmcp35ze.jpg" 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%2Fr8z23h1kcspsxmcp35ze.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Listed here are the naming standards:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For Classes, Interfaces/Contracts, Traits: use &lt;code&gt;PascalCase&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;For Constants: use &lt;code&gt;TITLE_CASE&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;For Functions/Methods, Class Properties, Variables: use &lt;code&gt;camelCase&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;For Array Indices/Database Field Names/Model Fillables/Model Relations: use &lt;code&gt;lower_snake_case&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;For Routes: use &lt;code&gt;lower-kebab-case&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&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%2Fr1p91dcbiwshjrdd15uo.gif" 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%2Fr1p91dcbiwshjrdd15uo.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Below are the standards use cases and samples :&lt;/p&gt;

&lt;p&gt;For Classes, Interfaces/Contracts, Traits: use&lt;code&gt;PascalCase&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Class:&lt;/strong&gt;&lt;/p&gt;

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

class AuthController extends Controller
{
    //
}


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Interface/Contracts&lt;/strong&gt;&lt;/p&gt;

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

interface LoginInterface 
{
    /**
}


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Traits&lt;/strong&gt;&lt;/p&gt;

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

trait Audit
{
    /**
}


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

&lt;/div&gt;

&lt;p&gt;For Constants: use &lt;code&gt;TITLE_CASE&lt;/code&gt;&lt;/p&gt;

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

namespace App\Constants;

class AppConstant {
    const DEFAULT_PAGE = 1;
    const DEFAULT_PAGE_LIMIT = 10;
    const MAX_PAGE_LIMIT = 100;
    const ALPHANUMERIC_CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    const NUMERIC_CHARACTERS = '0123456789';
    const ALPHA_CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    const UPPERCASE_ALPHA_CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    const LOWERCASE_ALPHA_CHARACTERS = 'abcdefghijklmnopqrstuvwxyz';
}


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

&lt;/div&gt;

&lt;p&gt;For Functions/Methods, Class Properties, Variables: use &lt;code&gt;camelCase&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Functions/Methods&lt;/strong&gt;&lt;/p&gt;

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

public function refreshToken() : JsonResponse {
        return $this-&amp;gt;loginService-&amp;gt;refreshToken();
    }


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Class Properties&lt;/strong&gt;&lt;/p&gt;

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

class AuthController extends Controller
{
    // these are the class properties
    protected $loginService;
    protected $logoutService;

}


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Variables&lt;/strong&gt;&lt;/p&gt;

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

public function __construct(LoginService $loginService, LogoutService $logoutService) {
        $this-&amp;gt;loginService = $loginService;
        $this-&amp;gt;logoutService = $logoutService;
    }


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

&lt;/div&gt;

&lt;p&gt;For Array Indices/Database Field Names/Model Fillables/Model Relations: use &lt;code&gt;lower_snake_case&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Array Indices&lt;/strong&gt;&lt;/p&gt;

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

foreach($age as $x =&amp;gt; $x_value) {
  return $x_value;
}


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Database Field Names&lt;/strong&gt;&lt;/p&gt;

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

 public function up()
    {
        Schema::create('audits', function (Blueprint $table) {
            $table-&amp;gt;id();
            $table-&amp;gt;string('user_type')-&amp;gt;nullable();
            $table-&amp;gt;unsignedBigInteger('user_id')-&amp;gt;nullable();
            $table-&amp;gt;index(['user_id', 'user_type']);
        });
    }


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Model Fillables&lt;/strong&gt;&lt;/p&gt;

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

protected $fillable = [
        'first_name',
        'last_name',
        'username',
        'email',
        'password',

    ];


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

&lt;/div&gt;

&lt;p&gt;For Routes: use &lt;code&gt;lower-kebab-case&lt;/code&gt;&lt;/p&gt;

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

Route::group(['middleware' =&amp;gt; 'auth:api'], function() {
        Route::post('refresh-token', [AuthController::class, 'refreshToken']);
        Route::post('logout', [AuthController::class, 'logout']);
    });


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

&lt;/div&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>bestpractices</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Rate limiting and Throttling - System Design (Explained)</title>
      <dc:creator>DaleLanto</dc:creator>
      <pubDate>Mon, 14 Feb 2022 15:43:31 +0000</pubDate>
      <link>https://forem.com/dalelantowork/rate-limiting-and-throttling-system-design-indepth-explained-3jpo</link>
      <guid>https://forem.com/dalelantowork/rate-limiting-and-throttling-system-design-indepth-explained-3jpo</guid>
      <description>&lt;p&gt;If you're here then you probably came from this &lt;a href="https://dev.to/dalelantowork/api-rate-limiting-and-throttling-in-system-design-for-beginners-1pk0"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do Applications Rate Limit?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Applications can use a variety of techniques to rate limit their clients. &lt;/p&gt;

&lt;p&gt;The basic outcome from the client side is the same though: if you exceed a certain number of requests per time window, your requests will be rejected and the API will throw you a &lt;code&gt;ThrottlingException&lt;/code&gt;. &lt;strong&gt;Throttling exceptions&lt;/strong&gt; indicate what you would expect – you’re either calling too much, or your rate limits are too low. Either or, you should slow down your rate of calling.&lt;/p&gt;

&lt;p&gt;The most popular rate limiting or throttling technique that I’ve encountered in the real world is the &lt;strong&gt;Token Bucket Algorithm&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In fact, its the most popular method used in Amazon Web Services APIs so its important to be familiar with it if you’re using AWS. Lets explore it below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Token Bucket Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Token Bucket Algorithm has two major components, &lt;code&gt;burst&lt;/code&gt; and &lt;code&gt;refill&lt;/code&gt; (sometimes called sustain). We define them below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;#1 Burst&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;corresponds to the number of &lt;code&gt;Tokens&lt;/code&gt; that are available for a client. The &lt;code&gt;Tokens&lt;/code&gt; are consumed every time a request comes in.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this example, imagine that a token is a 1:1 mapping to a drop of water in a bucket. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Burst&lt;/strong&gt; actually refers to the &lt;code&gt;size of the bucket&lt;/code&gt; (aka the number of drops that are within it available for consumption). &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%2Fipgpwum76q1dhaazye7j.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%2Fipgpwum76q1dhaazye7j.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The more burst capacity you have, the more receptive the server is of high volume, but low frequency traffic. &lt;/p&gt;

&lt;p&gt;Keep in mind here that each client has their own bucket.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;#2 Refill/Sustain&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;corresponds to the rate in which the backend service &lt;code&gt;refills&lt;/code&gt; water into your bucket. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is essentially the replenishment rate of how fast the backend will give you more opportunities to call the API.&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%2Fd0o222c857oo6k985n3f.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%2Fd0o222c857oo6k985n3f.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An important concept of the token bucket algorithm is the time unit used to define the burst/refill. &lt;/p&gt;

&lt;p&gt;Usually this operates in seconds for most respectable APIs. This means that your burst capacity is calculated on a per second basis (too many requests exceeding the burst rate in a single second will cause Throttling). &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%2Fk6bk1frpe21ypyfgnh39.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%2Fk6bk1frpe21ypyfgnh39.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, refill/sustain is usually on a per second basis (you receive &lt;code&gt;new tokens&lt;/code&gt; or &lt;code&gt;water&lt;/code&gt; on a per-second basis). &lt;/p&gt;

&lt;p&gt;The time unit here could be anything though from milliseconds to seconds to minutes to hours – its really up to you.&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%2Fno9uwk66pnnwlaebwqsu.jpg" 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%2Fno9uwk66pnnwlaebwqsu.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another thing to keep in mind is that the burst rate(&lt;code&gt;size&lt;/code&gt;) is usually &lt;strong&gt;greater than or equal&lt;/strong&gt; to the refill / sustain rate. &lt;/p&gt;

&lt;p&gt;Practically speaking, this makes sense – the bucket capacity we are pouring our water into is usually greater than the rate we are pouring water in. &lt;/p&gt;

&lt;p&gt;For example, if we have a bucket that is 1 Litre capacity, it wouldn’t make much sense to have a &lt;code&gt;sustain rate&lt;/code&gt; or &lt;code&gt;pouring rate&lt;/code&gt; of 2 Litres per second – it would mean that you’re pouring at a rate that is constantly making the bucket overflow which doesn’t make much sense.&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%2Fu5xrpj9caeheejrq0lzo.jpg" 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%2Fu5xrpj9caeheejrq0lzo.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So lets think about some different combinations of high/low burst and sustain limits and the implications it has on the client of a rate limited api.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;High Burst, Low Refill/Sustain&lt;/strong&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%2Fqgh89nd3c2v1dd7d2f35.jpg" 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%2Fqgh89nd3c2v1dd7d2f35.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This combination means that the client will be allowed to make infrequent, bursty calls to an API. However if the client has too many bursts before the bucket has gone back to maximum capacity, the next burst of calls will fail&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Equal Burst and Refill/Sustain&lt;/strong&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%2Fff9mepwxvvnsyoqd06v5.jpg" 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%2Fff9mepwxvvnsyoqd06v5.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If your burst is equivalent to your refill/sustain, you essentially have a static limit per time unit, i.e. 10 requests allowed per second. This configuration is more similar to the Time Window rate limiting algorithm.&lt;/p&gt;

&lt;p&gt;The main strength of the Token Bucket Algorithm is that it allows you to build an API that is accommodating of low frequency, bursty workloads. At the same time, you can prevent too many bursts from occurring from a single client by controlling their refill rate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implications of a Rate Limited API&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server Perspective&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;From the server perspective, Rate Limiting means that you need to either use existing rate limiting features in web servers, or build your own to control traffic. &lt;/p&gt;

&lt;p&gt;For example, here’s an example from NGINX showing a rate limited API by client IP address at a rate of 1 request / second:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http {
    #...
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using IP address is a bit ill advised here since its very easy for customers to change their IP addresses at well. &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%2Fhm8xkuagsu6grt766s1f.jpg" 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%2Fhm8xkuagsu6grt766s1f.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ideally, you would want to use some client identity name or access token to identify a client in order to control their rates. &lt;/p&gt;

&lt;p&gt;This is how it works with the Twitter API where with each request you attach your developer access token. This lets Twitter identify you consistently and apply rate limits to just you and nobody else.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Client Perspective&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you’re a Client, or a user of a rate limited API, there are some important things to be aware of. Most importantly, you as the client need to be aware of your rate limits. &lt;/p&gt;

&lt;p&gt;This helps you design your system in such a way that you won’t exceed the rates provisioned by your resource server.&lt;/p&gt;

&lt;p&gt;Secondly, its important to implement a robust retry policy with &lt;code&gt;exponential backoff&lt;/code&gt; when faced with a &lt;code&gt;ThrottlingException&lt;/code&gt; from a rate limited API.&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%2Fleulyymu2qr00q9c1cq9.jpg" 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%2Fleulyymu2qr00q9c1cq9.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the real world, I usually see a retry policy that consists of 3 attempts, with an exponentially increasing sleep duration between each attempts. &lt;/p&gt;

&lt;p&gt;This makes it such that your request takes a progressively longer sleep between each attempt, giving the resource server the opportunity to ‘catch up’ and assign you more tokens.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Its important to know what Rate Limiting / Throttling is from both the client and the server perspective. &lt;/p&gt;

&lt;p&gt;If you’re a developer using an open source API, I guarantee you that you will at some point be facing the dreaded &lt;code&gt;ThrottlingException&lt;/code&gt; or &lt;code&gt;RateLimitedExceedException&lt;/code&gt; from these APIs. Its important to know how to handle them in any case.&lt;/p&gt;

&lt;p&gt;If you’re a resource owner / service builder, Rate Limiting / Throttling is an important concept that helps regulate the resources of your service per client so that you can ensure a consist experience for ALL users. &lt;/p&gt;

&lt;p&gt;Its important to know how it works, and some of the algorithms that are available to you. The Token bucket is one of the most popular and wide-spread used.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>api</category>
      <category>systemdesign</category>
      <category>bestpractices</category>
      <category>beginners</category>
    </item>
    <item>
      <title>API Rate Limiting in System Design for Beginners (Simple and Easy)</title>
      <dc:creator>DaleLanto</dc:creator>
      <pubDate>Mon, 14 Feb 2022 15:26:31 +0000</pubDate>
      <link>https://forem.com/dalelantowork/api-rate-limiting-and-throttling-in-system-design-for-beginners-1pk0</link>
      <guid>https://forem.com/dalelantowork/api-rate-limiting-and-throttling-in-system-design-for-beginners-1pk0</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is Rate Limiting?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Rate limits are calculated in Requests Per Second (RPS). For example, let’s say a developer only wants to allow a client to call the API a maximum of 10 times per minute. &lt;/p&gt;

&lt;p&gt;In this case the developer would apply a rate limit to their API expressed as “10 requests per 60 seconds”. &lt;/p&gt;

&lt;p&gt;This means that the client will be able to successfully call the API up to 10 times within any 60 second interval and after that the user will get an error stating their rate limit has been exceeded if they call it an 11th time within that time frame.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why is this important in API?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the realm of API management, rate limiting is one of the fundamental aspects of managing traffic to your APIs. Rate limiting can easily become one of the easiest and most efficient ways to control traffic to your APIs.&lt;/p&gt;

&lt;p&gt;Rate limiting can help with API overuse caused by accidental issues within client code which results in the API being slammed with requests. &lt;/p&gt;

&lt;p&gt;On the malicious side, a denial of service attack meant to overwhelm the API resources can also be easily executed without rate limits in place.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Yes, we are talking about DDOS Attacks!&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%2Fwezzm444eq99xmwg7iqd.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%2Fwezzm444eq99xmwg7iqd.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All popular websites use rate limiting, here are some examples:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Github&lt;/strong&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%2F4n47nb8h5uczsv4bmzos.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%2F4n47nb8h5uczsv4bmzos.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bitly&lt;/strong&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%2Fcwe3dliq8vfngjbuiuxk.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%2Fcwe3dliq8vfngjbuiuxk.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Linked In&lt;/strong&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%2Fc4354yb0ialo9qdxpayv.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%2Fc4354yb0ialo9qdxpayv.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Twitter&lt;/strong&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%2Fwfme7rrsisel5nikn436.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%2Fwfme7rrsisel5nikn436.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In simple terms we can categorize rate limiting in two type:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;#1 API-level rate limiting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;API-level rate limiting assesses all traffic coming into an API from all sources and ensures that the overall rate limit is not exceeded. &lt;/p&gt;

&lt;p&gt;Overwhelming an endpoint with traffic is an easy and efficient way to execute a &lt;strong&gt;denial of service attack&lt;/strong&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%2F471ey63mk3en1jnckozd.jpg" 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%2F471ey63mk3en1jnckozd.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By using a global rate limit you can easily ensure that all incoming requests are within a specific limit. &lt;/p&gt;

&lt;p&gt;This limit may be calculated by something as simple as having a good idea of the maximum amount of requests you could expect from users of your API. &lt;/p&gt;

&lt;p&gt;It may also be something more scientific and precise like the amount of requests your system can handle while still performing at a high-level. &lt;/p&gt;

&lt;p&gt;This may be easily uncovered with some performance testing in order to establish this threshold.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;this is used on the server side&lt;/p&gt;

&lt;p&gt;An API-level global rate limit may be used as an extra line of defence around attempted denial of service attacks. For instance, if you have load tested your current system and established a performance threshold that you would not want to exceed to ensure system availability and/or performance then you may want to set a global rate limit as a defence to make sure that it is not exceeded&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;#2 Key-level rate limiting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Key-level rate limiting is more focused on &lt;strong&gt;controlling traffic from individual sources&lt;/strong&gt; and making sure that users are staying within their prescribed limits.&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%2F50u0an2gfrv3su2tkkiq.jpg" 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%2F50u0an2gfrv3su2tkkiq.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This approach to rate limiting allows you to configure a policy to rate limit in several ways: limiting the rate of calls the user of a key can make to all available APIs, another form of global rate limiting just from one specific user, and limiting the rate of calls to specific individual APIs, also known as a “per API rate limit”.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;this is used on the client side&lt;/p&gt;

&lt;p&gt;For key-level rate limiting you will be aiming to ensure that one particular user or system accessing the API is not exceeding a determined rate. This makes sense in a scenario such as APIs which are associated with a monetisation scheme where you may allow so many requests per second based on the tier in which that consumer is subscribed or paying for.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Great! You now understand what rate limiting is and the importance of it in APIs, if you want a more specific explanation on rate limiting click this &lt;a href="https://dev.to/dalelantowork/rate-limiting-and-throttling-system-design-indepth-explained-3jpo"&gt;link&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>api</category>
      <category>bestpractices</category>
      <category>systemdesign</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
