<?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: Junaid Javed</title>
    <description>The latest articles on Forem by Junaid Javed (@junaidjaved248).</description>
    <link>https://forem.com/junaidjaved248</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%2F421733%2F6e5dc4d3-fcc5-41fb-9d12-a3fee7f0c98a.jpeg</url>
      <title>Forem: Junaid Javed</title>
      <link>https://forem.com/junaidjaved248</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/junaidjaved248"/>
    <language>en</language>
    <item>
      <title>Using the booted Method in Laravel Eloquent Models for CRUD Event Listening and Cache Resetting</title>
      <dc:creator>Junaid Javed</dc:creator>
      <pubDate>Tue, 14 Mar 2023 05:36:19 +0000</pubDate>
      <link>https://forem.com/junaidjaved248/using-the-booted-method-in-laravel-eloquent-models-for-crud-event-listening-and-cache-resetting-4519</link>
      <guid>https://forem.com/junaidjaved248/using-the-booted-method-in-laravel-eloquent-models-for-crud-event-listening-and-cache-resetting-4519</guid>
      <description>&lt;h2&gt;
  
  
  Introduction to the booted method in Eloquent
&lt;/h2&gt;

&lt;p&gt;In Laravel's Eloquent ORM, the booted method is called when an Eloquent model is instantiated. This method provides a convenient place to register event listeners or perform other setup tasks.&lt;/p&gt;

&lt;p&gt;By using the booted method, you can keep your model logic organized and easy to maintain. In this post, we'll explore how to use the booted method to listen for CRUD events and reset cache when creating or updating a model.&lt;/p&gt;

&lt;p&gt;Lets understand it using example:&lt;/p&gt;

&lt;p&gt;Suppose you have a Laravel application with a &lt;code&gt;User&lt;/code&gt;model that is frequently accessed and updated. To speed up queries and reduce database load, you decide to cache user data using Laravel's Cache façade.&lt;/p&gt;

&lt;p&gt;To cache user data, you'll need to store it using a unique cache key. You could use the user's ID as the cache key, but what happens when the user's data changes? You'll need to update the cache key every time the user is updated to ensure that the cache is not stale.&lt;/p&gt;

&lt;p&gt;To automate this process, you can use the booted method to listen for the &lt;code&gt;creating&lt;/code&gt;and &lt;code&gt;updating&lt;/code&gt;events on the &lt;code&gt;User&lt;/code&gt;model, and reset the &lt;code&gt;cache&lt;/code&gt;for the user being created or updated.&lt;/p&gt;

&lt;h2&gt;
  
  
  Registering event listeners
&lt;/h2&gt;

&lt;p&gt;To listen for CRUD events, you can register event listeners using the &lt;code&gt;creating&lt;/code&gt;, &lt;code&gt;created&lt;/code&gt;, &lt;code&gt;updating&lt;/code&gt;, &lt;code&gt;updated&lt;/code&gt;, &lt;code&gt;saving&lt;/code&gt;, &lt;code&gt;saved&lt;/code&gt;, &lt;code&gt;deleting&lt;/code&gt;, and &lt;code&gt;deleted&lt;/code&gt; methods on the model's static boot method.&lt;/p&gt;

&lt;p&gt;In this example, we'll use the &lt;code&gt;creating&lt;/code&gt;and &lt;code&gt;updating&lt;/code&gt; events to reset cache for the user being created or updated.&lt;/p&gt;

&lt;p&gt;Here's how you can define event listeners in the boot method of the User model:&lt;br&gt;
&lt;/p&gt;

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

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class User extends Model
{
    protected static function booted()
    {

        static::creating(function ($user) {
            Cache::forget('user_' . $user-&amp;gt;id);
        });

        static::updating(function ($user) {
            Cache::forget('user_' . $user-&amp;gt;id);
        });
    }
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Resetting cache
&lt;/h2&gt;

&lt;p&gt;To reset cache for a model, you can use Laravel's Cache facade. The Cache facade provides a simple API for storing and retrieving data from a variety of cache backends.&lt;/p&gt;

&lt;p&gt;For example, to cache a user model by their ID, you can use the remember method of the Cache façade:&lt;br&gt;
&lt;/p&gt;

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

$user = Cache::remember('user_' . $userId, $minutes, function () use ($userId) {
    return User::find($userId);
});

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

&lt;/div&gt;



&lt;p&gt;In the above code, the remember method caches the user model with ID 1 for the specified number of minutes. If the cache is not already set, the closure passed to remember is called to retrieve the data and cache it.&lt;/p&gt;

&lt;p&gt;To reset cache for a user when they are created or updated, we use the forget method&lt;/p&gt;

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

&lt;p&gt;In this post, we've explored how to use the booted method in Eloquent models to listen for CRUD events and reset cache when creating or updating a model. By using event listeners and the Cache façade, you can keep your model logic organized and easy to maintain.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>php</category>
      <category>laravel</category>
      <category>blog</category>
    </item>
    <item>
      <title>orderByRaw Method In Laravel</title>
      <dc:creator>Junaid Javed</dc:creator>
      <pubDate>Mon, 27 Feb 2023 04:59:10 +0000</pubDate>
      <link>https://forem.com/junaidjaved248/orderbyraw-method-in-laravel-3310</link>
      <guid>https://forem.com/junaidjaved248/orderbyraw-method-in-laravel-3310</guid>
      <description>&lt;p&gt;The &lt;strong&gt;orderByRaw&lt;/strong&gt; method allows you to specify a raw SQL expression to use for sorting the results of a query. This can be useful when you need to sort by a calculated value, a complex expression, or a column that is not directly available in the database.&lt;/p&gt;

&lt;p&gt;Here's an example that demonstrates how to use orderByRaw in Laravel.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example # 1
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$users = DB::table('users')
    -&amp;gt;orderByRaw('LENGTH(name) DESC')
    -&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we first select all columns from the users table using the table method of the DB facade.&lt;/p&gt;

&lt;p&gt;We then use the orderByRaw method to specify a raw SQL expression to use for sorting the results. The expression LENGTH(name) calculates the length of the name column for each user, and the DESC keyword sorts the results in descending order based on this calculated value.&lt;/p&gt;

&lt;p&gt;Finally, we use the get method to retrieve the sorted results from the database.&lt;/p&gt;

&lt;p&gt;This will generate a SQL query that looks something 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;SELECT * FROM users ORDER BY LENGTH(name) DESC
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This query returns a list of all users, sorted in descending order based on the length of their names.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example # 2
&lt;/h2&gt;

&lt;p&gt;Let's assume we have a roles table with the following columns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id | name        | level
---|-------------|------
1  | Admin       | 3
2  | Moderator   | 2
3  | Contributor | 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You want to sort the roles based on their level of importance, rather than their alphabetical order. Here's how we can do that using orderByRaw:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$roles = DB::table('roles')
    -&amp;gt;orderByRaw('CASE WHEN level = 3 THEN 1 WHEN level = 2 THEN 2 ELSE 3 END')
    -&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, you can use the orderByRaw method to specify a raw SQL expression to use for sorting the roles. The expression uses a CASE statement to assign a numerical value to each role based on its level of importance. We assign the value 1 to roles with level 3, 2 to roles with level 2, and 3 to all other roles.&lt;/p&gt;

&lt;p&gt;The ORDER BY clause then sorts the roles based on these assigned values, with the roles with the lowest assigned value (i.e., the most important roles) appearing first in the result set.&lt;/p&gt;

&lt;p&gt;This will generate a SQL query that looks something 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;SELECT * FROM roles ORDER BY CASE WHEN level = 3 THEN 1 WHEN level = 2 THEN 2 ELSE 3 END
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This query returns a list of all roles, sorted based on their level of importance.&lt;/p&gt;

&lt;p&gt;Example # 3:&lt;br&gt;
Here's an example of a more complex orderByRaw expression in Laravel:&lt;/p&gt;

&lt;p&gt;Assume we have a sales table with columns &lt;code&gt;id, product_id, quantity, price, and created_at&lt;/code&gt;. We want to retrieve a list of the top 10 products by revenue for the current month, and include the total revenue and quantity sold for each product. We can do this using the following query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$topProducts = DB::table('sales')
    -&amp;gt;select('product_id', DB::raw('SUM(quantity) as total_quantity'), DB::raw('SUM(price * quantity) as total_revenue'))
    -&amp;gt;whereBetween('created_at', [Carbon::now()-&amp;gt;startOfMonth(), Carbon::now()-&amp;gt;endOfMonth()])
    -&amp;gt;groupBy('product_id')
    -&amp;gt;orderByRaw('total_revenue DESC')
    -&amp;gt;take(10)
    -&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the select method to select the &lt;code&gt;product_id&lt;/code&gt;, and calculate the &lt;code&gt;total_quantity&lt;/code&gt; and &lt;code&gt;total_revenue&lt;/code&gt;columns using the SUM and DB::raw methods.&lt;/p&gt;

&lt;p&gt;We then use the &lt;code&gt;whereBetween&lt;/code&gt; method to filter the results to only include sales made during the current month.&lt;/p&gt;

&lt;p&gt;Next, we use the &lt;code&gt;groupBy&lt;/code&gt;method to group the results by &lt;code&gt;product_id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We then use the &lt;code&gt;orderByRaw&lt;/code&gt;method to specify a raw SQL expression to use for sorting the results. The expression &lt;code&gt;total_revenue&lt;/code&gt;DESC sorts the results in descending order based on the &lt;code&gt;total_revenue&lt;/code&gt; column that we calculated in the select method.&lt;/p&gt;

&lt;p&gt;Finally, we use the take method to limit the results to the top 10 products by revenue, and use the get method to retrieve the results from the database.&lt;/p&gt;

&lt;p&gt;This will generate a SQL query that looks something 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;SELECT product_id, SUM(quantity) as total_quantity, SUM(price * quantity) as total_revenue  FROM sales 
WHERE created_at BETWEEN '2023-02-01 00:00:00' AND '2023-02-28 23:59:59' 
GROUP BY product_id 
ORDER BY total_revenue DESC 
LIMIT 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This query returns a list of the top 10 products by revenue for the current month, sorted based on the total revenue generated by each product. Each row in the result set includes the &lt;code&gt;product_id&lt;/code&gt;, &lt;code&gt;total_quantity&lt;/code&gt;, and &lt;code&gt;total_revenue&lt;/code&gt;for the corresponding product.&lt;/p&gt;

&lt;p&gt;Note that the expression passed to &lt;code&gt;orderByRaw&lt;/code&gt; should be a valid SQL expression and properly escaped to avoid SQL injection vulnerabilities. Also, if you need to sort by multiple columns or expressions, you can chain multiple &lt;code&gt;orderByRaw&lt;/code&gt; calls or use the &lt;code&gt;orderBy&lt;/code&gt;method instead.&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>Laravel Request Lifecycle Overview</title>
      <dc:creator>Junaid Javed</dc:creator>
      <pubDate>Tue, 21 Feb 2023 16:51:43 +0000</pubDate>
      <link>https://forem.com/junaidjaved248/laravel-request-lifecycle-overview-671</link>
      <guid>https://forem.com/junaidjaved248/laravel-request-lifecycle-overview-671</guid>
      <description>&lt;p&gt;Would you like to learn about the Request Life Cycle in Laravel? This Article refers to the series of stages through which the framework processes a given request, ultimately providing a response to the user. Let’s examine this process step-by-step to better understand it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fvxrc7payioail19ma8bo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvxrc7payioail19ma8bo.png" alt=" " width="800" height="1000"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Entry Point (Auto Loader)
&lt;/h2&gt;

&lt;p&gt;The starting point for all requests in a Laravel application is the public/index.php file. This file is responsible for directing all incoming requests to the correct location within the application.&lt;br&gt;
When a request is made, the index.php file first loads the auto-loader files created by Composer. This allows for the dynamic loading of classes and dependencies as needed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ftglc2j994zd4i3bvi6oq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ftglc2j994zd4i3bvi6oq.png" alt=" " width="800" height="83"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the auto-loader files have been loaded, the &lt;strong&gt;index.php&lt;/strong&gt; file retrieves an instance of the Laravel application from the &lt;strong&gt;bootstrap/app.php&lt;/strong&gt; script.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fk2runtj6xwm7wwb2qpfj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fk2runtj6xwm7wwb2qpfj.png" alt=" " width="800" height="95"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Kernel
&lt;/h2&gt;

&lt;p&gt;Once the application instance generated ✅&lt;br&gt;
The Incoming request will be handled by the kernel. There are two types of kernel in Laravel&lt;br&gt;
1) HTTP kernel&lt;br&gt;
2) Console kernel.&lt;br&gt;
Incoming Request can be handled by either HTTP kernel or Console kernel depending on the request type.&lt;br&gt;
HTTP kernel, which is placed in app/Http/Kernel.php&lt;br&gt;
It just receive a Request and return a Response.&lt;/p&gt;

&lt;p&gt;Bootstrappers that are defined by the Kernel class, which configures error handling, configure logging, detect environments and other tasks to be done before the request handled.&lt;br&gt;
HTTP kernel also Register &amp;amp; Execute Middleware’s (Manage HTTP session, Detect maintenance mode, verify CSRF token, etc.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Service Providers
&lt;/h2&gt;

&lt;p&gt;Next step of the kernel is to load service providers as part of the bootstrapping action. Providers that are needed for the application are placed in config/app.php configuration file.&lt;/p&gt;

&lt;p&gt;All of the service providers for the application are configured in the config/app.php configuration file’s providers array.&lt;/p&gt;

&lt;p&gt;First, the register method will be called on all providers, then, once all providers have been registered, the boot method will be called.&lt;/p&gt;

&lt;p&gt;Service providers are responsible for bootstrapping all of the framework’s various components, such as the database, queue, validation, and routing components.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bootstraping involves, Initializing configurations, detecting application environment, configure error handling, configure logging and other task needs to execute before request being handled&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Dispatch Request
&lt;/h2&gt;

&lt;p&gt;Once the application have been bootstrapped and all service providers are registered and booted, the Request will be handed over to the router for dispatching.&lt;/p&gt;

&lt;p&gt;The router will dispatch the request to a route or controller, as well as run any route specific middleware.&lt;/p&gt;

&lt;p&gt;The Router will handle the HTTP Request and either direct it to a Controller or return a view or response directly without a controller. These routes are defined in the app/routes.php file.&lt;/p&gt;

&lt;p&gt;The Controller, located at app/controllers/, performs specific actions and sends the data to a View.&lt;/p&gt;

&lt;p&gt;The View, located at app/views/, formats the data and sends it back as an HTTP Response.&lt;/p&gt;

&lt;p&gt;That’s All&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>Laravel Tip (Select Columns using eloquent all method)</title>
      <dc:creator>Junaid Javed</dc:creator>
      <pubDate>Sun, 23 Jan 2022 16:39:13 +0000</pubDate>
      <link>https://forem.com/junaidjaved248/laravel-tip-select-columns-using-eloquent-all-method-2cmp</link>
      <guid>https://forem.com/junaidjaved248/laravel-tip-select-columns-using-eloquent-all-method-2cmp</guid>
      <description>&lt;h2&gt;
  
  
  Select Columns through Laravel Eloquent
&lt;/h2&gt;

&lt;p&gt;Now we can select the columns to run the when using the Model::all() method. No Need to use additional select() method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Instead of this
$users = User::select(['id','name','email'])-&amp;gt;get();
//Use this one
$users = User::all(['id','name','email']);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>laravel</category>
      <category>model</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Deploy Laravel on AWS Elastic beanstalk</title>
      <dc:creator>Junaid Javed</dc:creator>
      <pubDate>Sat, 22 Jan 2022 18:23:20 +0000</pubDate>
      <link>https://forem.com/junaidjaved248/deploy-laravel-on-aws-elastic-beanstalk-in-5-minutes-5dl4</link>
      <guid>https://forem.com/junaidjaved248/deploy-laravel-on-aws-elastic-beanstalk-in-5-minutes-5dl4</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;In this quick tutorial, you will learn how to deploy a Laravel application on AWS elasticbeanstalk. By the end of the tutorial, you will be able to deploy a Laravel application on Elasticbeanstalk, update the application and also connect it to a database.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;AWS Elastic Beanstalk is an orchestration service offered from Amazon Web Services for deploying infrastructure which orchestrates various AWS services, including EC2, S3, Simple Notification Service, CloudWatch, auto scaling, and Elastic Load Balancers. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Install Laravel
&lt;/h1&gt;

&lt;p&gt;We create a fresh Laravel application using composer&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h1&gt;
  
  
  Start Laravel
&lt;/h1&gt;

&lt;p&gt;We will start Laravel on the local server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd `JJLaravelApp` &amp;amp;&amp;amp; php artisan serve
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Laravel Application should now be running on &lt;a href="http://localhost:8000" rel="noopener noreferrer"&gt;http://localhost:8000&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%2Fwkc7vo9msoyss83vdokm.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%2Fwkc7vo9msoyss83vdokm.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Setup AWS Elasticbeanstalk Environment
&lt;/h1&gt;

&lt;p&gt;If you do not already have an AWS account, you need to sign up &lt;a href="https://portal.aws.amazon.com/billing/signup#/start" rel="noopener noreferrer"&gt;here&lt;/a&gt;. This will give you access to Elasticbeanstalk and other AWS Services.&lt;/p&gt;

&lt;h1&gt;
  
  
  Create an Elasticbeanstalk Application
&lt;/h1&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%2F5ce9htmxhtaky5ex17no.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%2F5ce9htmxhtaky5ex17no.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click the Get Started button. You will be taken to an application creation page&lt;/p&gt;

&lt;p&gt;Give a name to your application. In this case, I have named mine jj-laravel-app. Also, select &lt;code&gt;PHP&lt;/code&gt; as the platform. Then select “Upload your code” in the application code section. &lt;/p&gt;

&lt;h1&gt;
  
  
  Prepare Laravel Application for Deployment
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Zip Laravel Application
&lt;/h2&gt;

&lt;p&gt;We have to create a zip file of our entire Laravel application files, excluding the vendor folder. Note that you should not zip the parent JJLaravelApp folder, instead create a zip of the application files themselves.&lt;/p&gt;

&lt;p&gt;Once the zip file is ready, click on the “Upload” button to upload a zip file containing your Laravel application code.&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%2Fs5l664mvyyyk4gqdvgsv.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%2Fs5l664mvyyyk4gqdvgsv.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will be prompted to either upload a local file or a public S3 URL. Choose “Local file” and click the “Upload” button to upload the zip file.&lt;/p&gt;

&lt;h1&gt;
  
  
  Deploy Laravel to Elasticbeanstalk Environment
&lt;/h1&gt;

&lt;p&gt;Now that the Zip file is successfully uploaded, click the “Create Application” button. Elasticbeanstalk will start provisioning the Laravel application.&lt;/p&gt;

&lt;p&gt;After a few minutes, the Laravel application should be successfully deployed.&lt;/p&gt;

&lt;p&gt;The top right corner of the dashboard shows a URL link to the deployed Laravel application. Click on it to navigate to the page.&lt;/p&gt;

&lt;p&gt;You should see a “403 Forbidden” error on the provided link, saying “permission to access / on the server isn’t granted”.&lt;/p&gt;

&lt;h1&gt;
  
  
  Update Application Root Folder
&lt;/h1&gt;

&lt;p&gt;We need to set the public folder as the root folder. Go back to the Application dashboard and click on “&lt;strong&gt;Configurations&lt;/strong&gt;”, then modify Software.&lt;/p&gt;

&lt;p&gt;Update the “Document root” to &lt;code&gt;/public&lt;/code&gt;, then click “Apply”. ElasticBeanstalk will then update the environment with the new configuration. That should take a few seconds.&lt;/p&gt;

&lt;p&gt;Now go back to the browser tab and refresh it.&lt;/p&gt;

&lt;p&gt;Our Laravel app is now Live!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update Laravel Application on ElasticBeanstalk&lt;/strong&gt;&lt;br&gt;
o you may be wondering, how do you update the deployed Laravel application.&lt;/p&gt;

&lt;p&gt;Lets make a quick update to the app.&lt;/p&gt;

&lt;p&gt;Go to your favorite IDE and edit the welcome screen (welcome.blade.php). Change “Laravel” word to “JJ Laravel ”, save it, then zip the files again.&lt;/p&gt;

&lt;p&gt;Now that we have zipped the updated Laravel app, Navigate to Elasticbeanstalk dashboard and click on the jj-laravel-app application. Select “Application Versions”. You will see the initially uploaded zip file.&lt;/p&gt;

&lt;p&gt;Click the “&lt;strong&gt;Upload&lt;/strong&gt;” button to upload a newer version of the application.&lt;/p&gt;

&lt;p&gt;Add a Version label, a description, then choose the newly updated zip file for the app, then click “&lt;strong&gt;Upload&lt;/strong&gt;”&lt;/p&gt;

&lt;p&gt;A new version of the Laravel app will now be added to the list of app versions. Elasticbeanstalk lets you choose which version of your application you may want to deploy at anytime. You can also choose to rollback to an earlier version of the app by simply redeploying the source.&lt;/p&gt;

&lt;p&gt;Select the new version and click “&lt;strong&gt;Deploy&lt;/strong&gt;”. Elasticbeanstalk will now begin the process of updating the Laravel application. Wait a few seconds and everything should be ready.&lt;/p&gt;

&lt;p&gt;Go back to the browser tab and refresh the page:&lt;/p&gt;

&lt;p&gt;We have successfully deployed a new version of the Laravel Application.&lt;/p&gt;

&lt;h1&gt;
  
  
  Connecting to a Database
&lt;/h1&gt;

&lt;p&gt;There are two ways to connect your application on Elasticbeanstalk to a database:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Using an &lt;strong&gt;internal RDS database&lt;/strong&gt;: AWS Elasticbeanstalk provides an internal database for your application, but with a caveat — the database gets deleted too if you delete your Elasticbeanstalk app environment. It is therefore more suitable for development and testing purposes, while its discouraged for production usage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using an &lt;strong&gt;external database&lt;/strong&gt;: Connect to an external database, be it a standalone RDS or a remote database, with required endpoints and credentials, through the environmental variables Elasticbeanstalk provides or through the &lt;code&gt;.env&lt;/code&gt; file in the Laravel application. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Provision an Internal Elasticbeanstalk RDS Database
&lt;/h1&gt;

&lt;p&gt;Go to the Application dashboard and click on “Configurations”, then modify Database.&lt;/p&gt;

&lt;p&gt;You are prompted to choose the type of database you will like provisioned. Select the &lt;strong&gt;Mysql&lt;/strong&gt; engine with a &lt;code&gt;t2.micro&lt;/code&gt; database instance class. Choose a username and a password with which you will access the database. Select delete for retention and set Availability to &lt;code&gt;Low&lt;/code&gt;. Now click “&lt;strong&gt;Apply&lt;/strong&gt;”. Elasticbeanstalk will begin to provision the database. This may take a few minutes.&lt;/p&gt;

&lt;p&gt;Once the database is provisioned, you will see a link to the database endpoint. You can now use this endpoint, together with the other credentials(username, password e.t.c) to connect to the database.&lt;/p&gt;

&lt;p&gt;The procedure to connect to the internal database is the same for connecting to the external database — pass the credentials to the environmental properties of the Application.&lt;/p&gt;

&lt;h1&gt;
  
  
  Connect Elasticbeanstalk Application to External/Internal Database
&lt;/h1&gt;

&lt;p&gt;The properties specified in the Elasticbeanstalk environmental properties usually overrides the value for the same property specified in the Laravel &lt;code&gt;.env&lt;/code&gt; environment file.&lt;/p&gt;

&lt;p&gt;Go to the Application dashboard and click on “&lt;strong&gt;Configurations&lt;/strong&gt;”, then modify Software. Scroll to the bottom:&lt;/p&gt;

&lt;p&gt;Specify the name-value pairs for the environment variables. This is where you provide the endpoint for the internal or external database and all other database credentials. Once that’s done, click “&lt;strong&gt;Apply&lt;/strong&gt;”.&lt;/p&gt;

&lt;p&gt;You have now successfully connected the Laravel Application on Elasticbeanstalk to a database.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;In this tutorial, we created a fresh Laravel application and deployed it to AWS Elasticbeanstalk. We also created and deployed different versions of this application. Lastly, we connected it to an internal and external database, through the software environment properties.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>phplaravel</category>
      <category>ec2</category>
      <category>elasticbeanstalk</category>
    </item>
    <item>
      <title>How To Deploy Laravel Project on EC2 AWS .								
</title>
      <dc:creator>Junaid Javed</dc:creator>
      <pubDate>Sat, 22 Jan 2022 16:32:59 +0000</pubDate>
      <link>https://forem.com/junaidjaved248/how-to-deploy-laravel-project-on-ec2-aws--5d8c</link>
      <guid>https://forem.com/junaidjaved248/how-to-deploy-laravel-project-on-ec2-aws--5d8c</guid>
      <description>&lt;h2&gt;
  
  
  Connect to EC2 instance using your private key ( Key Pair ).
&lt;/h2&gt;

&lt;h1&gt;
  
  
  Update your Libraries
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-get update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Install Apache2
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-get install apache2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now copy the IPv4 Public IP Address of your EC2 instance and hit on the browser. If you see a screen something like below , you are good to go. Apache server is working!&lt;/p&gt;

&lt;h1&gt;
  
  
  Install PHP and useful packages
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-get install php
$ php --version
$ sudo apt-get install php7.4-cli php7.4-common php7.4-curl php7.4-gd php7.4-json php7.4-mbstring php7.4-intl php7.4-mysql php7.4-xml php7.4-zip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Usually, when a user requests a directory from the web server, Apache will first look for a file named index.html. If you want to change Apache to serve php files rather than others, move index.php to first position in the dir.conf file as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo vi /etc/apache2/mods-enabled/dir.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will install mysql server but this will not prompt anything to configure mysql.&lt;/p&gt;

&lt;h1&gt;
  
  
  Configuring MySQL
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo mysql_secure_installation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way you can make some changes to your MySQL installation’s security options.&lt;/p&gt;

&lt;p&gt;Note that even though you’ve set a password for the root MySQL user, this user is not configured to authenticate with a password when connecting to the MySQL shell because it uses &lt;code&gt;auth_socket&lt;/code&gt; by default.&lt;/p&gt;

&lt;p&gt;In order to use a password to connect to MySQL as root, you will need to switch its authentication method from &lt;code&gt;auth_socket&lt;/code&gt; to &lt;code&gt;mysql_native_password&lt;/code&gt; by using following commands.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo mysql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To configure the root account to authenticate with a password, run the following &lt;strong&gt;ALTER USER&lt;/strong&gt; command. Note that this command will change the root password you set in &lt;strong&gt;mysql_secure_installation&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mysql&amp;gt; ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpassword';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, run FLUSH PRIVILEGES which tells the server to reload the grant tables and put your new changes into effect:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;FLUSH PRIVILEGES;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now You are done with setting up your mysql server on your EC2. you can use your credentials in your laravel project.&lt;/p&gt;

&lt;h1&gt;
  
  
  Install Composer
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('sha384', 'composer-setup.php') === 'e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
$ php composer-setup.php
$ php composer-setup.php --install-dir=bin --filename=composer
$ php -r "unlink('composer-setup.php');"
$ composer --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Install Git
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt-get install git-core
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Clone Your Repository
&lt;/h1&gt;

&lt;p&gt;Change to &lt;strong&gt;/var/www/html&lt;/strong&gt; directory and clone your Laravel Project there using following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd /var/www/html
$ sudo git clone https://github.com/username/reponame.git
$ cd reponame
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Adding .env File to your Laravel project
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo vi .env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Installing Libraries In Project
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo composer install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Read Write Permissions for your Project
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo chmod 777 -R storage/
sudo chmod 777 -R bootstrap/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Apache Configuration
&lt;/h1&gt;

&lt;p&gt;By Default Apache open &lt;code&gt;/var/www/html/index.html&lt;/code&gt; Now we have to open our Laravel project when we go to the public IP of the EC2.&lt;br&gt;
Go to &lt;code&gt;/etc/apache2/sites-available/000-default.conf&lt;/code&gt; and change the DocumentRoot as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo vi /etc/apache2/sites-available/000-default.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lGjdAgxl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1pw1u2gw8ohzxpnn25n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lGjdAgxl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1pw1u2gw8ohzxpnn25n.png" alt="Image description" width="682" height="57"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Restart your Apache
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo systemctl restart apache2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you have successfully deployed your laravel Project on AWS ,&lt;br&gt;
Which you can easily access through your public IPV4 IP Address.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>awsdeployment</category>
      <category>webdev</category>
      <category>deployment</category>
    </item>
  </channel>
</rss>
