<?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: Parth Patel</title>
    <description>The latest articles on Forem by Parth Patel (@parthp1808).</description>
    <link>https://forem.com/parthp1808</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%2F176069%2F3dfe26ae-1714-4505-b43e-6d63621265c1.jpg</url>
      <title>Forem: Parth Patel</title>
      <link>https://forem.com/parthp1808</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/parthp1808"/>
    <language>en</language>
    <item>
      <title>Laravel 8 Authentication using Bootstrap 4 instead of Tailwind</title>
      <dc:creator>Parth Patel</dc:creator>
      <pubDate>Thu, 08 Oct 2020 00:00:00 +0000</pubDate>
      <link>https://forem.com/parthp1808/laravel-8-authentication-using-bootstrap-4-instead-of-tailwind-2kaj</link>
      <guid>https://forem.com/parthp1808/laravel-8-authentication-using-bootstrap-4-instead-of-tailwind-2kaj</guid>
      <description>&lt;p&gt;Laravel 8 has released with a bang! and with it , brought sweeping changes. One such remarkable yet controversial change is &lt;strong&gt;Laravel Jetstream&lt;/strong&gt;. Laravel Jetstream is a beautifully designed application scaffolding and comes with two stacks for Authentication system -&lt;/p&gt;

&lt;p&gt;1) &lt;strong&gt;Tailwind CSS +&lt;/strong&gt;  &lt;strong&gt;Livewire&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;2) &lt;strong&gt;Tailwind CSS + Inertia.js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, these two stacks are great, beautiful but very different from Bootstrap/jQuery based stack which developers are used to. So, there is a learning curve for developers if they want to use Laravel's Auth scaffolding which is sad because Laravel does not comes with Bootstrap based Auth scaffolding anymore.&lt;/p&gt;

&lt;p&gt;But, that's the beauty of &lt;strong&gt;Open Source&lt;/strong&gt; world. Anyone can modify these systems to their liking or build new plugins or integrations.&lt;/p&gt;

&lt;p&gt;Today, we will learn how we can still use &lt;strong&gt;Bootstrap&lt;/strong&gt; based Auth Scaffolding for new &lt;strong&gt;Laravel 8&lt;/strong&gt; app. We will be using our favourite &lt;strong&gt;Laravel/UI&lt;/strong&gt; package to bring back old Bootstrap based Auth Scaffolding.&lt;/p&gt;

&lt;h2&gt;
  
  
  1) Create Laravel Project
&lt;/h2&gt;

&lt;p&gt;If you have installed &lt;strong&gt;Laravel Installer&lt;/strong&gt; (You can follow instructions from our &lt;a href="https://dev.to/laravel-tutorial-for-beginner/"&gt;Laravel Beginner Tutorial&lt;/a&gt;) then simply execute 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;laravel new laravelauth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;else, execute 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 create-project laravel/laravel --prefer-dist laravelauth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then go to your project folder :&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  2) Install Laravel/UI
&lt;/h2&gt;

&lt;p&gt;Laravel UI is an official package that offers basic Auth Scaffolding built on the Bootstrap CSS framework. Latest version of Laravel/UI supports Laravel 8 so we will use this package to import authentication pages, controllers, and other components.&lt;/p&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  3) Install Bootstrap in Laravel
&lt;/h2&gt;

&lt;p&gt;Laravel UI offers three options for Auth Scaffolding - 1) Bootstrap, 2) Vue and 3) React. So, we will install bootstrap for our case.&lt;/p&gt;

&lt;p&gt;Run 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;php artisan ui bootstrap --auth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can observe that we have used &lt;strong&gt;—auth&lt;/strong&gt; flag here. Without that flag, above command will only install bootstrap and basic scaffolding. The &lt;strong&gt;—auth&lt;/strong&gt; flag installs login and registration scaffolding as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  4) Install NPM packages
&lt;/h2&gt;

&lt;p&gt;We have configured bootstrap and other related dependancies in &lt;strong&gt;package.json&lt;/strong&gt; in previous step, so now we will actually install it.&lt;/p&gt;

&lt;p&gt;Before moving forward, make sure you have &lt;strong&gt;node&lt;/strong&gt; installed in your system. You can check node version via 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;# for node
node -v

# for npm
npm -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's install the dependencies and compile our assets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install &amp;amp;&amp;amp; npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5) Database Migration
&lt;/h2&gt;

&lt;p&gt;First, we should configure our database settings. In your &lt;strong&gt;.env&lt;/strong&gt; file located at the root of your project, udpate the database config as per your system. [If you don't find .env file, simply create new one and add the credentials as per the example below] :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we will create database tables via migration by executing 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;php artisan migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6) Run Development Server
&lt;/h2&gt;

&lt;p&gt;Finally, it's time to test your Laravel Application with Bootstrap based scaffolding. Sometimes, OLD IS GOLD.&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 serve
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Screenshots:
&lt;/h3&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%2Fi%2Fug1yrt469sv1596j6leq.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%2Fi%2Fug1yrt469sv1596j6leq.png" alt="Alt Text"&gt;&lt;/a&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%2Fi%2Fb73h9xrl8k6cgmtq8k1w.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%2Fi%2Fb73h9xrl8k6cgmtq8k1w.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope this article helped you. There is no clear winner in Bootstrap vs Tailwind. It's all about preference. Since, Laravel 8 prefers Tailwind with Livewire or Inertia stack, it might be confusing for developers who wants to use Bootstrap instead. So, start working on your next project.&lt;/p&gt;

&lt;p&gt;Adios!&lt;/p&gt;

&lt;p&gt;For more tips &amp;amp; tutorial, &lt;a href="https://parthpatel.substack.com/subscribe" rel="noopener noreferrer"&gt;join my newsletter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
    </item>
    <item>
      <title>15 Must know Laravel Collection Methods ~ Laravel Collections Tutorial</title>
      <dc:creator>Parth Patel</dc:creator>
      <pubDate>Sun, 06 Sep 2020 22:02:39 +0000</pubDate>
      <link>https://forem.com/parthp1808/15-must-know-laravel-collection-methods-laravel-collections-tutorial-36ok</link>
      <guid>https://forem.com/parthp1808/15-must-know-laravel-collection-methods-laravel-collections-tutorial-36ok</guid>
      <description>&lt;p&gt;Laravel comes with very powerful and convenient wrapper for working with arrays. You must have used collections in Laravel specially while working with Eloquent. &lt;strong&gt;Eloquent&lt;/strong&gt; returns the query results in custom Collection object ~ Eloquent Collection class is inherited from &lt;strong&gt;Laravel Collections&lt;/strong&gt; therefore, it inherits all the powerful methods offered by &lt;a href="https://www.parthpatel.net/laravel/"&gt;Laravel&lt;/a&gt; Collection and on top, provides few more great methods to use.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AjWhBV6B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/www.parthpatel.net/wp-content/uploads/2020/09/Laravel.jpg%3Fresize%3D900%252C375%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AjWhBV6B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/www.parthpatel.net/wp-content/uploads/2020/09/Laravel.jpg%3Fresize%3D900%252C375%26ssl%3D1" alt="Laravel collection methods"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article, we will review some of the best and must-know &lt;strong&gt;Laravel Collection methods&lt;/strong&gt; to make your life easier working with Laravel.&lt;/p&gt;

&lt;p&gt;_ &lt;strong&gt;Note&lt;/strong&gt; : Remember, Eloquent Collection inherits Laravel Base Collection class thus it has access to all of the Base Collection's methods but Base Collection object cannot access custom methods provided by eloquent collection. _&lt;/p&gt;

&lt;p&gt;Let's look at how you can create simple Laravel Base Collection Object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$todocollection = collect([
    [
        'user_id' =&amp;gt; '1',
        'title' =&amp;gt; ‘Do Laundry,
        ‘description =&amp;gt; ‘I ‘have to do laundry ‘
    ],
    [
         'user_id' =&amp;gt; '1',
        'title' =&amp;gt; ‘Finish Assignment,
        ‘description =&amp;gt; ‘I have to finish Maths assignment ‘
    ],
]);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is how you can create Laravel Collection object. You can use all the powerful methods provided by &lt;a href="https://laravel.com/docs/7.x/collections"&gt;Laravel Collection&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, let's look at an example of Eloquent Collection object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$todos = ToDo::where('user_id',1)-&amp;gt;get();

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



&lt;p&gt;Here, $todos is an eloquent collection object which contains query results. It is simply an array from inside with some powerful collection methods at your disposal. You can still use &lt;a href="https://laravel.com/docs/7.x/collections"&gt;Laravel Collection&lt;/a&gt; methods but Eloquent Collection also provides &lt;a href="https://laravel.com/docs/7.x/eloquent-collections"&gt;additional methods&lt;/a&gt;. You can do various things like converting to array, filtering your collection, sorting, mapping etc.&lt;/p&gt;

&lt;p&gt;Let's look at some of the best and must-know Laravel Collection Methods:&lt;/p&gt;

&lt;h3&gt;
  
  
  1)  all()
&lt;/h3&gt;

&lt;p&gt;Laravel Collection class has a protected property called  &lt;strong&gt;$items&lt;/strong&gt; which contains an array with all the data. Collection wrapper provides additional methods to operate on that array. But if you simply want the array without all those bells and whistles then there is a method for that!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$emails = collect([
        ‘abc@gmail.com,
        ‘user@yahoo.com’,
        ‘example@example.com’,
    ‘test@yahoo.co.in’
    ]);

    $emails_array = $emails-&amp;gt;all();

    dd($emails_array);

/*
Array:4 [▼
  0 =&amp;gt; "‘abc@gmail.com"
  1 =&amp;gt; "user@yahoo.com"
  2 =&amp;gt; "example@example.com’"
  3 =&amp;gt; “test@yahoo.co.in”
]
*/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  2) avg()
&lt;/h3&gt;

&lt;p&gt;Avg method provided by Laravel Collection returns the average value. By default, it will provide average of all the values in collection. But, if collection contains key =&amp;gt; value pair, then you can provide key to the function and it will automatically calculate the average of all values to that key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$simple_collection = collect([2,5,7,35,25,10]);

$s_avg = $simplex_collection-&amp;gt;avg();

// 14

$key_collection = collect(['score' =&amp;gt; 98], ['score' =&amp;gt; 90], ['score' =&amp;gt; 70],['score' =&amp;gt; 86]);

$key_avg = $key_collection-&amp;gt;avg();

//86
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  3) each()
&lt;/h3&gt;

&lt;p&gt;Each method for Laravel Collection is similar to map function on arrays in PHP and JavaScript. Each method basically takes each element from the collection and applies the callback function on it. This method can be used to apply operations on each element, modify each element etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$collection = collect([1,2,3,4]);

$collection-&amp;gt;each(function($item){
    return $item*$item;
});

// [1,4,9,16]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  4) chunk()
&lt;/h3&gt;

&lt;p&gt;chunk() method is used to split the Laravel Collection into multiple chunks of a given size.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$users = User::all();
$userschunk = $users-&amp;gt;chunk(2);

dd($userschunk-&amp;gt;toArray());

array:3 [▼
  0 =&amp;gt; array:2 [▼
    0 =&amp;gt; array:3 [▼
      "id" =&amp;gt; 1
      "name" =&amp;gt; "John Doe"
      "email" =&amp;gt; "johndoe@example.com"
    ]
    1 =&amp;gt; array:3 [▼
      "id" =&amp;gt; 2
      "name" =&amp;gt; "Jack Halt"
      "email" =&amp;gt; "jhalt@example.com"
    ]
  ]
  1 =&amp;gt; array:1 [▼
    2 =&amp;gt; array:3 [▼
      "id" =&amp;gt; 3
      "name" =&amp;gt; "Sam Pitt"
      "email" =&amp;gt; "spitt@example.com"
    ]
  ]
]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;Also Read: *&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.parthpatel.net/laravel-tutorial-for-beginner/"&gt;Laravel 7 Tutorial for Beginner: Create your First To-Do App&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.parthpatel.net/laravel-scheduler-tutorial/"&gt;How to Schedule tasks with cron jobs using Laravel Scheduler&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5) contains()
&lt;/h3&gt;

&lt;p&gt;Often, you will need to check whether a collection contains a value or not. The Laravel Collection provides easy method to check that.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;contains()&lt;/em&gt; function is used to check whether the laravel collection contains the value passed to it as a parameter. It will return a boolean value -- true if it contains and false if not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$users = collect(['name' =&amp;gt; 'John Doe', 'email' =&amp;gt; "johndoe@example.com"]);

$users-&amp;gt;contains('johndoe@example.com');

// true

$users-&amp;gt;contains('spitt@example.com');

// false
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It can also accept pair of key and value for more granular search. It will check whether the given pair exists or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$users = collect(['name' =&amp;gt; 'John Doe', 'email' =&amp;gt; "johndoe@example.com"]); 

$users-&amp;gt;contains('email', 'johndoe@example.com'); 

// true 

$users-&amp;gt;contains('name', 'johndoe@example.com'); 

// false
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can also pass a callback function with your own condition to check whether any value passes the given condition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$collection = collect([11, 25, 30, 45, 50]);

$collection-&amp;gt;contains(function ($value, $key) {
    return $value == 55;
});

// false
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;&lt;em&gt;contains()&lt;/em&gt;&lt;/strong&gt; method will use loose comparison to apply the condition. For more strict comparison, you can use _*&lt;em&gt;containsStrict() *&lt;/em&gt;_method.&lt;/p&gt;

&lt;h3&gt;
  
  
  6) count() &amp;amp; countBy()
&lt;/h3&gt;

&lt;p&gt;This is very simple yet very helpful method provided by Laravel Collection. How many times you need to count the items in Laravel Collection or an array?&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;&lt;strong&gt;Count()&lt;/strong&gt;&lt;/em&gt; &lt;em&gt;method gives the total number of items in given Laravel Collection.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$collection = collect([10,20,30,40,50]);

$collection-&amp;gt;count();

// 5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Laravel collection also provides more advanced method to count its items.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;&lt;strong&gt;CountBy()&lt;/strong&gt;&lt;/em&gt; &lt;em&gt;method gives the count of item's occurrences in given Laravel Collection in array.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$numbercollection = collect([10, 30,30, 50, 50]);

$counts = $numbercollection-&amp;gt;countBy();

$counts-&amp;gt;all();

// [10 =&amp;gt; 1, 30 =&amp;gt; 2, 50 =&amp;gt; 2]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  7) filter()
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;_filter() _&lt;/strong&gt;method is one of the most popular and useful Laravel Collection method. It allows you to filter your collection using the passed callback function. It returns the new collection with filtered items and does not change the original collection.&lt;/p&gt;

&lt;p&gt;The callback function is provided with _ &lt;strong&gt;key ** __and  **value&lt;/strong&gt; _ as its parameters which you can utilize to create the filter condition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$users = collect(['name' =&amp;gt; 'John Doe', 'email' =&amp;gt; "johndoe@example.com"]);

$filter = $users-&amp;gt;filter(function($value, $key) {
    if ($value['email'] == "johndoe@example.com") {
        return true;
    }
});

$filter-&amp;gt;all();
/*
[
    1 =&amp;gt;[
               'name' =&amp;gt; 'John Doe', 
               'email' =&amp;gt; "johndoe@example.com"
     ]
]
*/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you don't provide any callback function, then it will simply remove any items equivalent to _ &lt;strong&gt;false&lt;/strong&gt; __. _So, values like **null, false, ", 0, [] **will be filtered out. This method is easiest way to weed out any empty items in the collection.&lt;/p&gt;

&lt;h3&gt;
  
  
  8) get()
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;get()&lt;/strong&gt;&lt;/em&gt;_ _is a method which returns the value at the key you pass to it as a parameter. If no key is passed then null is returned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$users = collect(['name' =&amp;gt; 'John Doe', 'email' =&amp;gt; "johndoe@example.com"]);

$value = $users-&amp;gt;get('name');

// John Doe
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  9) search()
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;search()&lt;/strong&gt;&lt;/em&gt;_ _is another small yet helpful Laravel Collection method. It searches the Collection for the given value and returns the index if found. If it's not found then  &lt;strong&gt;false&lt;/strong&gt; will be returned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$numbers = collect([31,21,53,64,27]);

$numbers-&amp;gt;search(27);

// 4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  10) groupBy()
&lt;/h3&gt;

&lt;p&gt;Often you will need to group the data as per certain data. Normally, you would group at database level, but still there are many use-cases where you might need to further group the data in collection.&lt;/p&gt;

&lt;p&gt;Laravel Collections offer handy method just for that. It works similar to Eloquent's groupBy and groups the items by given key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$orders = collect([
    ['product_id' =&amp;gt; 'p10', 'email' =&amp;gt; 'jdoe@example.com'],
    ['product_id' =&amp;gt; 'p10', 'email' =&amp;gt; 'billy@example.com'],
    ['product_id' =&amp;gt; 'p11', 'email' =&amp;gt; 'danny@example.com'],
]);

$grouped = $orders-&amp;gt;groupBy('product_id');

$grouped-&amp;gt;toArray();

/*
    [
        'p10' =&amp;gt; [
            ['product_id' =&amp;gt; 'p10', 'email' =&amp;gt; 'jdoe@example.com'],
            ['product_id' =&amp;gt; 'p10', 'email' =&amp;gt; 'billy@example.com'],
        ],
        'p11' =&amp;gt; [
            ['product_id' =&amp;gt; 'p11', 'email' =&amp;gt; 'danny@example.com'],
        ],
    ]
*/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  11) max()
&lt;/h3&gt;

&lt;p&gt;In Excel, you may used this function many times to get the maximum value in a column or row. Similarly, Laravel Collections provides **max() **function to get the maximum value of given key among collection items.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$counter = collect([['count' =&amp;gt; 10], ['count' =&amp;gt; 20]]);

$counter-&amp;gt;max('count');
// 20
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you don't pass any key to &lt;strong&gt;max()&lt;/strong&gt; function, it will simply compare each item and return the highest one.&lt;/p&gt;

&lt;h3&gt;
  
  
  12) pluck()
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;pluck()&lt;/strong&gt; is one of my favourite and most used method for Laravel Collection and more frequently for &lt;strong&gt;Eloquent&lt;/strong&gt; Collection. It allows us to fetch values for given key from the collection in array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$users = collect([
['name' =&amp;gt; 'John Doe', 'email' =&amp;gt; "johndoe@example.com"],
['name' =&amp;gt; 'Jack Sparrow', 'email' =&amp;gt; "sparrow@example.com"]
]);

$emails = $users-&amp;gt;pluck('email');

/*
["johndoe@example.com","sparrow@example.com"]
*/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  13) forget()
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;forget()&lt;/strong&gt; is a laravel collection method which simply removes given key and its value from all the items in the collection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$users = collect(['name' =&amp;gt; 'John Doe', 'email' =&amp;gt; 'johndoe@example.com']);

$users-&amp;gt;forget('name');

$users-&amp;gt;all();

// ['email' =&amp;gt; 'johndoe@example.com']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  14) toArray()
&lt;/h3&gt;

&lt;p&gt;**toArray() **is a straightforward Laravel Collection method to convert your collection object to a plain php array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$users = collect(['name' =&amp;gt; 'John Doe', 'email' =&amp;gt; 'johndoe@example.com']);

$users-&amp;gt;toArray();

/*
    [
        ['name' =&amp;gt; 'John Doe', 'email' =&amp;gt; 'johndoe@example.com'],
    ]
*/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  15) sortBy()
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;sortBy()&lt;/strong&gt; is a very helpful method to sort your Laravel Collection by given key. It retains the original keys, just reorders the items in the collection as per the sorting condition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$products = collect([
    ['name' =&amp;gt; 'Laptop', 'price' =&amp;gt; 2000],
    ['name' =&amp;gt; 'Mouse', 'price' =&amp;gt; 100],
    ['name' =&amp;gt; 'Keyboard', 'price' =&amp;gt; 150],
]);

$sortedproducts = $products-&amp;gt;sortBy('price');

/*
    [       
        ['name' =&amp;gt; 'Mouse', 'price' =&amp;gt; 100],
        ['name' =&amp;gt; 'Keyboard', 'price' =&amp;gt; 150],
        ['name' =&amp;gt; 'Laptop', 'price' =&amp;gt; 2000],
    ]
*/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For more advanced sorting, you can also pass a callback function which will handle how to sort the items.&lt;/p&gt;

&lt;p&gt;In a nutshell, these are best Laravel Collections methods you must know as a Laravel developer. There are lot more methods offered by Laravel Collections which you can checkout at &lt;a href="https://laravel.com/docs/7.x/collections"&gt;Laravel Documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://www.parthpatel.net/laravel-collection-methods-tutorial/"&gt;15 Must know Laravel Collection Methods ~ Laravel Collections Tutorial&lt;/a&gt; appeared first on &lt;a href="https://www.parthpatel.net"&gt;Parth Patel - a Web Developer&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>laravelcollection</category>
    </item>
    <item>
      <title>Career Advice: PHP vs Python vs JS?</title>
      <dc:creator>Parth Patel</dc:creator>
      <pubDate>Mon, 13 Apr 2020 14:39:44 +0000</pubDate>
      <link>https://forem.com/parthp1808/career-advice-php-vs-python-vs-js-g7p</link>
      <guid>https://forem.com/parthp1808/career-advice-php-vs-python-vs-js-g7p</guid>
      <description>&lt;p&gt;I am a Laravel/Vue full stack developer with 2+ years of experience. While searching for new job, I observed that big companies or small but exciting companies tend to use technologies like React, Python, C# etc. And that sort of demotivated me as I am only efficient in Laravel with bit of experience in Vue.&lt;/p&gt;

&lt;p&gt;Should I learn and focus on these technologies? (I love working with Laravel but I want to work for company which has exciting product to work on or bigger companies (everyone fancies working for BIG companies I guess)&lt;/p&gt;

&lt;p&gt;If yes,&lt;br&gt;
then I am confused between Python or JS (React mostly as vue is not being used much by bigger companies)&lt;/p&gt;

&lt;p&gt;Also, I have experience with only web development which I love but don't know about other development.&lt;br&gt;
My Dilemma comes down to my wish for working for great and exciting company for long term rather than jumping from one startup to another&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>php</category>
      <category>javascript</category>
      <category>python</category>
    </item>
    <item>
      <title>Laravel 7: How to Clear Cache using Artisan Command</title>
      <dc:creator>Parth Patel</dc:creator>
      <pubDate>Fri, 20 Mar 2020 17:28:46 +0000</pubDate>
      <link>https://forem.com/parthp1808/laravel-7-how-to-clear-cache-using-artisan-command-24kf</link>
      <guid>https://forem.com/parthp1808/laravel-7-how-to-clear-cache-using-artisan-command-24kf</guid>
      <description>&lt;p&gt;In your Laravel Application, clear cache easily with these quick and helpful artisan commands.&lt;/p&gt;

&lt;p&gt;In this quick tutorial, I will mention few artisan commands you can use to clear various types of cache like config cache, view cache, route cache and application cache from your laravel application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uUm5Zg34--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.parthpatel.net/wp-content/uploads/2020/03/laravel-cache-clear-1024x610.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uUm5Zg34--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.parthpatel.net/wp-content/uploads/2020/03/laravel-cache-clear-1024x610.png" alt="laravel cache clear"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This can be useful and sometimes essential while working on your Laravel application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Clear Cache in Laravel using Terminal
&lt;/h2&gt;

&lt;p&gt;Using Terminal, go to the Laravel application's folder. Then execute the following commands as needed to clear cache from laravel.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
#### Clear Application Cache&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To clear laravel application cache, run the following artisan command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan cache:clear
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
#### Clear Route Cache&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To clear route cache of your Laravel application run the following artisan command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan route:clear
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
#### Clear Configuration Cache&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To clear the config cache of your laravel application, run the following artisan command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan config:clear
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
#### Clear Compiled Views Cache&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To clear the view cache of your laravel application which are basically compiled view files, run the following artisan command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan view:clear
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Sometimes, it's hard to get console access to your laravel application. Thus, it would be easier if we could clear cache of laravel application through browser, right? No worries, we can do that too!&lt;/p&gt;

&lt;p&gt;Also, this method is useful for users who are using  &lt;strong&gt;shared hosting&lt;/strong&gt; for their laravel application! Since it's bit complex to run artisan commands via terminal in shared hosting, we can and will use browser method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Clear Cache in Laravel using Browser
&lt;/h2&gt;

&lt;p&gt;Here, we are still using same artisan commands as mentioned above. It's just that, those commands can be run programmatically in Laravel. Thus, we will create special routes to clear cache in Laravel.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Clear application cache:
 Route::get('/clear-cache', function() {
     $exitCode = Artisan::call('cache:clear');
     return 'Application cache has been cleared';
 });

//Clear route cache:
 Route::get('/route-cache', function() {
     $exitCode = Artisan::call('route:cache');
     return 'Routes cache has been cleared';
 });

 //Clear config cache:
 Route::get('/config-cache', function() {
     $exitCode = Artisan::call('config:cache');
     return 'Config cache has been cleared';
 }); 

 // Clear view cache:
 Route::get('/view-clear', function() {
     $exitCode = Artisan::call('view:clear');
     return 'View cache has been cleared';
 });
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Also Read:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.parthpatel.net/error-laravel-6-php-artisan-makeauth-command-not-defined-solved/"&gt;[Error] Laravel 6 php artisan make:auth command not defined! [Solved]&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.parthpatel.net/laravel-tutorial-for-beginner/"&gt;Laravel 7 Tutorial for Beginner: Create your First To-Do App&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The post &lt;a href="https://www.parthpatel.net/laravel-clear-cache-artisan/"&gt;Laravel 7: How to Clear Cache using Artisan Command&lt;/a&gt; appeared first on &lt;a href="https://www.parthpatel.net"&gt;Parth Patel - a Web Developer&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>laravel</category>
    </item>
    <item>
      <title>Python: How to Print Without Newline</title>
      <dc:creator>Parth Patel</dc:creator>
      <pubDate>Sat, 14 Mar 2020 02:36:56 +0000</pubDate>
      <link>https://forem.com/parthp1808/python-how-to-print-without-newline-5bcj</link>
      <guid>https://forem.com/parthp1808/python-how-to-print-without-newline-5bcj</guid>
      <description>&lt;p&gt;Python is one of the most easiest language yet most powerful language to learn. Normally, one would start with &lt;strong&gt;print&lt;/strong&gt;"hello world". No, you cannot start learning without that! Never!!&lt;/p&gt;

&lt;p&gt;In python, you can print any value using print function. Yes! It's that simple. But there is one interesting problem. In most of the languages, if you want to print something on newline, you have to specifically specify that. Otherwise, it will always print in current line and not move to next/new line.&lt;/p&gt;

&lt;p&gt;But, python by default always prints to newline. Now, that might become problem if you actually want to print on same line, isn't it? No worries. Let's see the solutions together.&lt;/p&gt;

&lt;p&gt;Since, python has two popular but distinct versions ~ Python 2.x and Python 3.x, let's look into the solutions separately.&lt;/p&gt;

&lt;h3&gt;
  
  
  Print without newline in Python 3
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1) Using print function
&lt;/h4&gt;

&lt;p&gt;In python 3, you can print multiple times without newline by simply overriding end parameter in the print function. Basically, by default Python appends newline to print function but you can override it with empty string and it won't print to newline.&lt;/p&gt;

&lt;p&gt;Let's see one example:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print("Hello World", end="")
print(" Again!")
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello World Again!)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's try one more different example. We will use loop to print multiple integers separated by "." (and not newline ). Here, we are using end parameter to append "." at the end of print statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# python print on same line separated by "."
for i in range(1,10):
    print(i, end='.')
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  2) Using "sys" Library
&lt;/h4&gt;

&lt;p&gt;Okay. Honestly, I cannot think of any situation where you would prefer this over print function, but let's explore this solution at least for fun. You can import and use sys library in python 3 to print strings or variables in same line avoiding newline.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Import the inbuilt sys library
import sys
#First Line of output
sys.stdout.write("Hello World")
#Second Line of output
sys.stdout.write(" Again!")
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Print without newline in Python 2
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1) Using print function
&lt;/h4&gt;

&lt;p&gt;In python2, you can use print statement (yes, print is not a function rather a statement in python 2.x) with comma to prevent newline.&lt;/p&gt;

&lt;p&gt;Let's see example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print "Hello World",
print " Again!"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is how you can print without newline in python 3 using  &lt;strong&gt;end&lt;/strong&gt; argument and python 2 using &lt;strong&gt;comma&lt;/strong&gt;. I hope you find it useful. Let me know in comments how you are using this trick.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Also Read:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.parthpatel.net/python-how-to-check-if-string-contains-substring/"&gt;Python: How to check if string contains substring&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.parthpatel.net/python-if-list-contains-value/"&gt;Python : How to check if list contains value&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The post &lt;a href="https://www.parthpatel.net/python-how-to-print-without-newline/"&gt;Python: How to Print Without Newline&lt;/a&gt; appeared first on &lt;a href="https://www.parthpatel.net"&gt;Parth Patel - a Web Developer&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>faq</category>
      <category>python</category>
    </item>
    <item>
      <title>PHP: How to generate a Unique id in PHP (Alphanumeric String)</title>
      <dc:creator>Parth Patel</dc:creator>
      <pubDate>Tue, 25 Feb 2020 20:12:26 +0000</pubDate>
      <link>https://forem.com/parthp1808/php-how-to-generate-a-unique-id-in-php-alphanumeric-string-533e</link>
      <guid>https://forem.com/parthp1808/php-how-to-generate-a-unique-id-in-php-alphanumeric-string-533e</guid>
      <description>&lt;p&gt;In php, there are many ways to generate &lt;strong&gt;unique alphanumeric id&lt;/strong&gt;. First of all, we will understand why we might need unique alphanumeric id in first place.&lt;/p&gt;

&lt;p&gt;In applications, normally we use unique integer ids which is generally generated by database itself ~ Auto-increment, unique integers. But sometimes we need unique alphanumeric string as ids instead of simple integers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Few Reasons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unique ID is needed if you have multiple-server app architecture i.e -- Your database is stored in multiple servers&lt;/li&gt;
&lt;li&gt;If you want to merge different branches of data without clashes&lt;/li&gt;
&lt;li&gt;Unique ID is helpful and must if you want to prevent sequence discovery of data in application or API endpoints.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Generate Unique Alphanumeric String using &lt;strong&gt;uniqid()&lt;/strong&gt; function in PHP
&lt;/h3&gt;

&lt;p&gt;In PHP, there is a very helpful function &lt;strong&gt;uniqid()&lt;/strong&gt; which generates unique alphanumeric string for you. The function has two parameters ~ though both are optional --&lt;/p&gt;

&lt;p&gt;1) $prefix -- you can prefix the generated id with any string&lt;br&gt;&lt;br&gt;
2) $more_entropy -- By default, it sets to  &lt;strong&gt;FALSE&lt;/strong&gt; but if set to  &lt;strong&gt;TRUE&lt;/strong&gt; , it will add additional entropy ~ basically it means that it will increase the uniqueness of the returned string&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//generates 13 character random unique alphanumeric id
echo uniqid();
//output - 5e6d873a4f597

//generates 13 character random unique id with prefix - db
echo uniqid("db");
//output - db5e6d875de7ba5

//generates 23 character random unique id with prefix - db but with more randomness
echo uniqid("db",true);
//output - db5e6d8782d90365.80737996

//generates random unique id with random prefix - higher randomness than fixed prefix
echo uniqid (rand (),true);
//output - 5e6d88383c9abdb5e6d88383c9b118314536965e6d88383c9b75.70966391
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see above, we can use &lt;strong&gt;uniqid()&lt;/strong&gt; function in different ways to generate unique id.&lt;/p&gt;

&lt;h3&gt;
  
  
  Generate Unique Alphanumeric String using &lt;strong&gt;random_bytes()&lt;/strong&gt; function in PHP
&lt;/h3&gt;

&lt;p&gt;In &lt;strong&gt;PHP 7.x&lt;/strong&gt; , there is another powerful and more secure function to generate random alphanumeric string -- &lt;strong&gt;random_bytes()&lt;/strong&gt;. It requires only one parameter -- length, and it generates cryptographically secure pseudo-random bytes. Further, passing its output to another function &lt;strong&gt;bin2hex()&lt;/strong&gt;, will give you unique random string.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$bytes = random_bytes(16);
echo bin2hex($bytes);
//output - 68309ba352806f9a943e16e74e5a9da3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Thus, using any of the above methods, you can create unique random strings and use it instead of basic integers and thus secure your application. Hope this helped you out.&lt;/p&gt;

&lt;p&gt;Adios&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://www.parthpatel.net/php-generate-unique-id-string/"&gt;PHP: How to generate a Unique id in PHP (Alphanumeric String)&lt;/a&gt; appeared first on &lt;a href="https://www.parthpatel.net"&gt;Parth Patel - a Web Developer&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>faq</category>
      <category>php</category>
    </item>
    <item>
      <title>How to find nearby places using Latitude and longitude in Laravel 5</title>
      <dc:creator>Parth Patel</dc:creator>
      <pubDate>Sat, 19 May 2018 22:23:15 +0000</pubDate>
      <link>https://forem.com/parthp1808/how-to-find-nearby-places-using-latitude-and-longitude-in-laravel-5-4iih</link>
      <guid>https://forem.com/parthp1808/how-to-find-nearby-places-using-latitude-and-longitude-in-laravel-5-4iih</guid>
      <description>&lt;p&gt;Often, in real-estate , dating  and other similar websites, you might have noticed how they allow you to search nearby to any location. You can easily accomplish that in your laravel application using Haversine formula.&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%2Fwww.parthpatel.net%2Fwp-content%2Fuploads%2F2018%2F05%2Fnearby-location-in-laravel-1024x512.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%2Fwww.parthpatel.net%2Fwp-content%2Fuploads%2F2018%2F05%2Fnearby-location-in-laravel-1024x512.png" alt="nearby location in laravel"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Source -- Tighten.co&lt;/p&gt;

&lt;p&gt;Last year, I worked on real-estate project in my college as an capstone project and that's when I got to work on this query. Though haversine formula is easy to find but to use it in laravel through Eloquent was very difficult. I tried many solutions found in Stackoverflow, laracasts forum and other sources but couldn't make them work. Some of them did work but since they were using raw queries, I couldn't paginate the results which is what I needed.&lt;/p&gt;

&lt;p&gt;After some trial and error, I finally make it working. So, here I am sharing the code snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  $gr_circle_radius = 6371;
      $max_distance = 500;

      $distance_select = sprintf(
                                    "           
                                    ( %d * acos( cos( radians(%s) ) " .
                                            " * cos( radians( lat ) ) " .
                                            " * cos( radians( long ) - radians(%s) ) " .
                                            " + sin( radians(%s) ) * sin( radians( lat ) ) " .
                                        " ) " . 
                                    ")
                                     ",
                                    $gr_circle_radius,               
                                    $lat,
                                    $long,
                                    $lat
                                   );

        $properties = Property::select('*')
        -&amp;gt;having(DB::raw($distance_select), '&amp;lt;=', $max_distance)
        -&amp;gt;groupBy('properties.id')-&amp;gt;paginate(1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, I have set maximum distance = 500 Km so it will show all results within 500 Km of the given Latitude and Longitude (used $lat, $long as variables).&lt;/p&gt;

&lt;p&gt;To see the Demo, Check out my real estate project -- &lt;a href="http://firstfloor.parthweb.com/" rel="noopener noreferrer"&gt;FirstFloor&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://www.parthpatel.net/nearby-places-using-latitude-and-longitude-in-laravel-5/" rel="noopener noreferrer"&gt;How to find nearby places using Latitude and longitude in Laravel 5&lt;/a&gt; appeared first on &lt;a href="https://www.parthpatel.net" rel="noopener noreferrer"&gt;Parth Patel - a Web Developer&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>codesnippet</category>
      <category>laravel</category>
    </item>
    <item>
      <title>How to create CSS hoverable side navigation menu</title>
      <dc:creator>Parth Patel</dc:creator>
      <pubDate>Fri, 08 Sep 2017 22:11:40 +0000</pubDate>
      <link>https://forem.com/parthp1808/how-to-create-css-hoverable-side-navigation-menu-5h2b</link>
      <guid>https://forem.com/parthp1808/how-to-create-css-hoverable-side-navigation-menu-5h2b</guid>
      <description>&lt;p&gt;In this guide, I will explain how to create a vertical side menu using Bootstrap, and CSS and optionally Javascript. Even if you are a beginner, you will be able to implement this and create a hoverable vertical navigation menu with ease.&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%2Fwww.parthpatel.net%2Fwp-content%2Fuploads%2F2017%2F09%2Fvertical-menu.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%2Fwww.parthpatel.net%2Fwp-content%2Fuploads%2F2017%2F09%2Fvertical-menu.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are multiple ways to implement hoverable vertical menu though it depends on some factors.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Is the number of sub-items under each menu item equal?&lt;/li&gt;
&lt;li&gt;Do you want to use Only css or can use jquery plugin too?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Don't worry if this questions confuse you. Later, when you will see the different examples, you will understand the meaning of these questions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Method #1– Create Vertical Side Hoverable menu with equal number of sub-items using only CSS
&lt;/h3&gt;

&lt;p&gt;This is the most flexible vertical menu with great user experience though one downside is, you can only keep equal number of sub-items under each menu item which is not usual case generally.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 1 – HTML Markup
&lt;/h4&gt;

&lt;p&gt;At first, we will create a basic structure of our vertical menu with 3 menu items and 3-3 sub-items in each menu items.&lt;/p&gt;

&lt;p&gt;Note that, here I am using fontawesome too but you are not required to use it. If you do want to use fontawesome icons then don't forget to add font-awesome.css file in header.&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;nav&amp;gt;
    &amp;lt;div class="menu-item"&amp;gt;
      &amp;lt;h4&amp;gt;&amp;lt;a href="#"&amp;gt;&amp;lt;i class='fa fa-gear'&amp;gt;&amp;lt;/i&amp;gt; Settings&amp;lt;/a&amp;gt;&amp;lt;/h4&amp;gt;
      &amp;lt;ul&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Account&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Profile&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Security&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
      &amp;lt;/ul&amp;gt;
    &amp;lt;/div&amp;gt;

    &amp;lt;div class="menu-item"&amp;gt;
      &amp;lt;h4&amp;gt;&amp;lt;a href="#"&amp;gt;&amp;lt;i class='fa fa-question-circle'&amp;gt;&amp;lt;/i&amp;gt; Help&amp;lt;/a&amp;gt;&amp;lt;/h4&amp;gt;
      &amp;lt;ul&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;FAQ&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Submit a ticket&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Network Status&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
      &amp;lt;/ul&amp;gt;
    &amp;lt;/div&amp;gt;

    &amp;lt;div class="menu-item"&amp;gt;
      &amp;lt;h4&amp;gt;&amp;lt;a href="#"&amp;gt;&amp;lt;i class='fa fa-envelope'&amp;gt;&amp;lt;/i&amp;gt; Contact&amp;lt;/a&amp;gt;&amp;lt;/h4&amp;gt;
      &amp;lt;ul&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Phone&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Email&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Location&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
      &amp;lt;/ul&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/nav&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 2 – Menu Layout
&lt;/h4&gt;

&lt;p&gt;We will start by removing some default margin. Then we will add some basic font styles, margin, and width to vertical navigation menu. We will keep menu width to 200px.&lt;/p&gt;

&lt;p&gt;For menu headers a.k.a top level menu items, we will set basic styles like color, font-size, background, border-bottom etc. This will give header a unique distinct look keeping it different from sub-menu items.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* {
  margin: 0px;
  padding: 0px;
}

nav {
  font-family: Helvetica, Arial, "Lucida Grande", sans-serif;
  margin: 50px auto; 
  width: 200px;
}

.menu-item {
  width: 200px; 
}

/*Menu Header Styles*/
.menu-item h4 {
  color: #fff;
  font-size: 15px;
  font-weight: 500;
  padding: 7px 12px;
  background: #b90329;
  border-bottom: 1px solid white;
}
.menu-item h4 a {
  color: white;
  display: block;
  text-decoration: none;
  width: 200px;
}

.menu-item h4:hover {  
  background: #cc002c; 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 3 – Sub Menu
&lt;/h4&gt;

&lt;p&gt;Here first, we are targeting the sub-menu as whole. We will set the background color of submenu as white. Then we will set other basic styling like font-size, line-height , padding etc. Here you can see that we set the height of ul element to 0. This means, by default submenu will be hidden. We will show submenu on hover only. Also we will set list-style-type to none.&lt;/p&gt;

&lt;p&gt;For animation, we are using height animation which will increase the height of submenu using animation. Here we set the height of submenu on hover to 93px. But in your case, this value might be different. If you are using more than 3 sub-menu items or less then 3 items then you have to set accordingly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/*ul Styles*/
.menu-item ul {
  background: #fff;
  font-size: 13px;
  line-height: 30px;
  height: 0px;
  list-style-type: none;
  overflow: hidden;
  padding: 0px;

  /*Animation*/
  -webkit-transition: height 1s ease;
     -moz-transition: height 1s ease;
       -o-transition: height 1s ease;
      -ms-transition: height 1s ease;
          transition: height 1s ease;
}
.menu-item:hover ul {
  height: 93px;
}
.menu-item ul a {
  margin-left: 15px;
  text-decoration: none;
  color: #aaa;
  display: block;
  width: 200px;
}
/*li Styles*/
.menu-item li {
  border-bottom: 1px solid #eee;
}

.menu-item li:hover {
  background: #eee;
}

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://www.parthpatel.net/demo/menu-1.html" rel="noopener noreferrer"&gt;Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One important thing to note here is that we can’t just set the height to auto because for some reason this disables the animation. If we could set the height to auto, we could use different numbers of sub items under menu items. That's why, I will also show another method to display unequal number of sub items in vertical menu using hover.&lt;/p&gt;

&lt;h3&gt;
  
  
  Method #2 – Create Side Hoverable menu using CSS and Javascript
&lt;/h3&gt;

&lt;p&gt;This is another method to create vertical expanding menu using CSS, jQuery and another jQuery plugin called &lt;a href="https://github.com/briancherne/jquery-hoverIntent" rel="noopener noreferrer"&gt;jQuery-hoverIntent.js&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With this method, there is no limitation of keeping child items of equal numbers. Thus making it a great way to implement the hover expanding functionality but the catch is you need to load special jquery plugin to achieve that. That should not be an issue in normal circumstances.&lt;/p&gt;

&lt;p&gt;Ohk so let's see one example.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 1 – HTML Markup
&lt;/h4&gt;

&lt;p&gt;First, we will create a basic structure which will contain a vertical menu with 3 menu items and unequal number of child items -- a normal occurrence.&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;div id="container"&amp;gt;
  &amp;lt;ul id="nav1"&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Portfolio&amp;lt;/a&amp;gt;
    &amp;lt;ul&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Web&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Print&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Other&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Print&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Other&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;/ul&amp;gt;
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;About&amp;lt;/a&amp;gt;
      &amp;lt;ul&amp;gt;
      &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;History&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Meet The Owners&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
        &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Awards&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;/ul&amp;gt;
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Menu Heading 3&amp;lt;/a&amp;gt;
    &amp;lt;ul&amp;gt;
      &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Stage1&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
      &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Stage2&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
      &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Stage3&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
      &amp;lt;li&amp;gt;&amp;lt;a href="#"&amp;gt;Stage4&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;/ul&amp;gt;
    &amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 2 -- Menu Layout (CSS)
&lt;/h4&gt;

&lt;p&gt;Now we will use some css to design it a bit. This is just a demo so you can use any css styles you want to style it as per your requirements.&lt;/p&gt;

&lt;p&gt;Do notice that we are hiding the child item list by default. We will use jquery plugin to show them on hover in next step.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ul,
li,
a {
  padding: 0px;
  margin: 0px;
}

#nav1 a {
  color: #FFFFFF;
}

#nav1 li ul li a:hover {
  background-color: #394963;
}

div ul li ul {
  background-color: #4a5b78;
  list-style: none
}

#nav1 &amp;gt; li &amp;gt; a {
  background-color: #343434;
  padding: 16px 18px;
  text-decoration: none;
  display: block;
  border-bottom: 2px solid #212121;
  background: linear-gradient(top, #343434, #111111);
}

#nav1 li ul li a {
  padding: 10px 0;
  padding-left: 30px;
  text-decoration: none;
  display: block;
}

div {
  background-color: #000000;
  background-color: #343434;
  width: 280px;
}
/* Hide Dropdowns by Default */
#nav1 li ul {
  display: none;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 3 -- Hover Expanding Menu using jQuery
&lt;/h4&gt;

&lt;p&gt;Here we are using jQuery as well as jQuery plugin -- jquery.hoverintent.js to make the child menu slidedown and slideup on hover. We could have done this with only jquery without the need of loading another jquery plugin but it leads to flickering issue. Thus, this plugin exists to resolve that issue.&lt;/p&gt;

&lt;p&gt;You can get the jquery-hoverintent.js file from &lt;a href="https://github.com/briancherne/jquery-hoverIntent" rel="noopener noreferrer"&gt;here&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;$(document).ready(function() {
  $("#nav1 li").hoverIntent(
    function() {
      $(this).find('ul').slideDown();
    },
    function() {
      $(this).find('ul').slideUp();
    });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://codepen.io/parthp1808/pen/KvXJmX" rel="noopener noreferrer"&gt;Demo&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;There are some other tricks to achieve the similar effect but they have some user experience issues. Personally I found these methods so far best compared to others.&lt;/p&gt;

&lt;p&gt;If you know any other better way to create such hoverable vertical expanding menu then please comment below. I would love to add that here.&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://www.parthpatel.net/create-vertical-hover-expandable-menu/" rel="noopener noreferrer"&gt;How to create CSS hoverable side navigation menu&lt;/a&gt; appeared first on &lt;a href="https://www.parthpatel.net" rel="noopener noreferrer"&gt;Parth Patel - a Web Developer&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>html</category>
      <category>frontenddev</category>
    </item>
    <item>
      <title>Object Oriented Programming in PHP for Beginner Series</title>
      <dc:creator>Parth Patel</dc:creator>
      <pubDate>Wed, 17 May 2017 02:02:20 +0000</pubDate>
      <link>https://forem.com/parthp1808/object-oriented-programming-in-php-for-beginner-series-2m47</link>
      <guid>https://forem.com/parthp1808/object-oriented-programming-in-php-for-beginner-series-2m47</guid>
      <description>&lt;p&gt;Object-oriented programming is relatively complex topic in php language. Thus, novice programmers find it difficult to understand php oop specially if they don’t have much programming experience.&lt;/p&gt;

&lt;p&gt;Object Oriented Programming was introduced from PHP 4 but it (php oops) gained momentum with PHP 5 version. In 5&lt;sup&gt;th&lt;/sup&gt; version, PHP’s Object model was rewritten which allowed more features with better performance and introduced full object model for php classes.&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%2Fwww.parthpatel.net%2Fwp-content%2Fuploads%2F2017%2F05%2Fphp-oop-tutorial-1024x576.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%2Fwww.parthpatel.net%2Fwp-content%2Fuploads%2F2017%2F05%2Fphp-oop-tutorial-1024x576.jpg" alt="php oop tutorial"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this tutorial series, you will gain enough knowledge about PHP OOPs or Object Oriented Programming in PHP to start with.&lt;/p&gt;

&lt;p&gt;If you want to learn about php frameworks then you can check our guide on &lt;a href="https://www.parthpatel.net/laravel-tutorial-for-beginner-5-4/" rel="noopener noreferrer"&gt;Laravel tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Object Oriented Programming?
&lt;/h2&gt;

&lt;p&gt;Object-oriented programming is a programming style based on the concept of objects. It helps developers by preventing &lt;em&gt;reinventing the wheel&lt;/em&gt; and make the code easy to maintain as well as scale.&lt;/p&gt;

&lt;p&gt;Object Oriented Programming seems confusing at first but when you go deep, you will realise that it is meant to simplify programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use Object Oriented Programming?
&lt;/h2&gt;

&lt;p&gt;There are various advantages of using PHP OOP over procedural programming in PHP like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Re-Usability and Recycling of code&lt;/strong&gt; :Let’s say you want to create Mercedes car object while your friends want to create Audi car object. You build your objects but you find that both objects have lot of similarities between Mercedes and Audi car like number of tires. The fact is both actually are car. So instead of created those two objects separately from scratch, if you have one car class with generic properties and methods, you can save lot of repetition by reusing and recycling the code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Modularity&lt;/strong&gt; : If you are creating multiple separate classes for different entities, you are making it modular as if you want to fix something, you only have to edit that specific class and not others. This modularity proves helpful in troubleshooting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalable&lt;/strong&gt; : Object Oriented Programs are more scalable than structured programs. An object’s interface provides all the information needed to replace the object thus it makes it easy to replace old code or add new code to scale in future.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintenance&lt;/strong&gt; : Programs needs to be improved on regular basis with many changes. An Object-Oriented program is much easier to modify and maintain than a non-Object Oriented Program.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Understanding Classes and Objects:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is Class and Object in PHP OOP?
&lt;/h3&gt;

&lt;p&gt;Before we dive further in Object Oriented Programming world, let’s learn about php classes and objects which are the pillars of PHP OOP paradigm.&lt;/p&gt;

&lt;p&gt;A class is a programmer-defined data type which contains different types of data as well as different functions.&lt;/p&gt;

&lt;p&gt;An object is an instance of a class. Object gets the copy of all the data and functions in the class thus it acts as independent entity.&lt;/p&gt;

&lt;p&gt;Let’s understand classes and objects using an example.&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%2Fwww.parthpatel.net%2Fwp-content%2Fuploads%2F2017%2F05%2Fclass-and-objects-php.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%2Fwww.parthpatel.net%2Fwp-content%2Fuploads%2F2017%2F05%2Fclass-and-objects-php.jpg" alt="class and objects php"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Left side of the above image shows car blueprint and right side shows cars created using that blueprint like Mercedes, BMW, Audi etc.&lt;/p&gt;

&lt;p&gt;Thus here, Car is a php class which acts like a blueprint and it contains data properties like number of tires, car type, car color etc.&lt;/p&gt;

&lt;p&gt;And Mercedes, Audi are known as objects which we created using class ‘Car’.&lt;/p&gt;

&lt;p&gt;All the objects were created from same class and thus they have same functions and properties but they may have different values of their properties. For example, in above image, all cars have different values to their color property – Mercedes is green; BMW is blue; Audi is Orange.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is method and property?
&lt;/h3&gt;

&lt;p&gt;Properties in a class are variables which can store data of different data types. Method is a function defined inside a class.&lt;/p&gt;

&lt;p&gt;All the objects instantiated from that class gets copy of that property variable and a copy of all the member functions aka Methods.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to create class and object?
&lt;/h3&gt;

&lt;p&gt;In class, we will group the code which handles a particular topic. Thus, for naming the class, we should use singular noun like Car which will handle logic about the cars.&lt;/p&gt;

&lt;p&gt;The syntax to create class is very easy and straightforward. You have to use &lt;strong&gt;&lt;em&gt;class&lt;/em&gt;&lt;/strong&gt; keyword to declare a class followed by the name of the class.&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

class Car
{
       // Class properties and methods
}

?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;Declare class using keyword &lt;strong&gt;&lt;em&gt;class&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Use singular noun and Capitalize the first letter&lt;/li&gt;
&lt;li&gt;If class name contains more than one word then capitalize each word using &lt;strong&gt;upper camel case&lt;/strong&gt; naming convention. For example: &lt;strong&gt;&lt;em&gt;JapaneseCar, AmericanCar&lt;/em&gt;&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;An instance of a class is an object created from existing class.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;new&lt;/strong&gt; keyword is used to create an object from class.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, you can create an object from class very easily using &lt;strong&gt;&lt;em&gt;new&lt;/em&gt;&lt;/strong&gt; keyword.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;$bmw = new Car;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Thus putting it all together:&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

class Car
{
               // Class properties and methods
}

$bmw = new Car;

?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to define properties in class?
&lt;/h3&gt;

&lt;p&gt;To add and store data to a class, properties or member variables are used. Properties can store values of data types – string, integer and decimal numbers, Boolean values etc. These are exactly like regular variables but they are bound to the instantiated object and thus you can access the variable only using the object.&lt;/p&gt;

&lt;p&gt;Let’s add some properties to the class Car.&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

class Car
{
               public $color = ‘red’;
               public $numberOfTires = 4;
}

$bmw = new Car;

?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;We used &lt;strong&gt;&lt;em&gt;public&lt;/em&gt;&lt;/strong&gt; keyword before class property to determine the visibility of the property about which we will discuss later.&lt;/li&gt;
&lt;li&gt;Class property can have default value. Also you can skip the default value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here you can see that we added some properties which are necessary for every car object. Obviously, car class needs lot more properties than this but for the tutorial, this is enough.&lt;/p&gt;

&lt;p&gt;Now to read any property, access the property using object. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo $bmw-&amp;gt;color;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The arrow (-&amp;gt;) is an OOP construct that accesses the properties and methods of a given object.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to define methods in class?
&lt;/h3&gt;

&lt;p&gt;The PHP functions which are defined in a class are called methods. Methods in PHP OOP are exactly similar to normal php functions.&lt;/p&gt;

&lt;p&gt;Let’s create some methods for our &lt;strong&gt;Car&lt;/strong&gt; class.&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

class Car
{
              public $color = ‘red’;
              public $numberOfTires = 4;

              public function setColor($val)
              {
                              $this-&amp;gt;color = $val;
              }

              public function getColor()
              {
                              return $this-&amp;gt;color;
              }
}

$bmw = new Car;

?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we created two methods 1) setColor – which will assign the given parameter to object’s color property and 2) getColor – which will return the value of object’s color property.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; $this is used to reference the current object. You can access class properties and methods from within the class using $this keyword.&lt;/p&gt;

&lt;p&gt;Now, to use these methods, call these methods using arrow “-&amp;gt;”. You need to reference corresponding objects because every instantiated objects have copy of these properties and methods.&lt;/p&gt;

&lt;p&gt;Let’s use these newly created methods.&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

class Car
{
    public $color = ‘red’;
        public $numberOfTires = 4;

        public function setColor($val)
    {
        $this-&amp;gt;color = $val;
    }

    public function getColor()
    {
        return $this-&amp;gt;color;
    }
}

$bmw = new Car;
$bmw-&amp;gt;setColor(“black”);
echo $bmw-&amp;gt;getColor();

?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;A code for the method is to be enclosed within a pair of curly braces.&lt;/li&gt;
&lt;li&gt;You need to use one of the keyword &lt;strong&gt;public, protected&lt;/strong&gt; or &lt;strong&gt;private&lt;/strong&gt; to create a class method or property. These are visibility keywords. More about this you will read later.&lt;/li&gt;
&lt;li&gt;If you haven’t used one of the keyword – public, protected or private then default is &lt;strong&gt;public&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You can also access properties and methods of the current instance or object using $this keyword -- $this-&amp;gt;color.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Constructor and Destructor
&lt;/h2&gt;

&lt;p&gt;In php there are some methods known as magic methods. Among them, two most important magic methods are __construct and __destruct. These two are methods to create constructor and destructor for the class. Remember, magic methods always start with two underscores.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is constructor and destructor?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The __construct method is a method which is called by the class when an object is being created. It is used to prepare the new object for use. Constructors can be used to assign parameter values to the object properties.&lt;/p&gt;

&lt;p&gt;The __destruct method is a function which is called by the class when an object is being destroyed. It is generally used to clean up memory as object is being destroyed.&lt;/p&gt;

&lt;p&gt;We will create a constructor to set value of the Color property.&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

class Car
{   
    public $color = ‘red’;    
    public $numberOfTires = 4;

    public function __construct($color)
    {        
        $this-&amp;gt;color = $color;        
    }

    public function setColor($val)
    {       
        $this-&amp;gt;color = $val;   
    }

    public function getColor()
    {        
        return $this-&amp;gt;color;    
    }  
}

$bmw = new Car(“white”);

echo $bmw-&amp;gt;getColor(); //this will print “white” because our constructor assigned value “white” to color property

$bmw-&amp;gt;setColor(“black”); // this will overwrite color property to black

echo $bmw-&amp;gt;getColor();

?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, we created a constructor which accepts one argument for color and sets it to the color property. We also changed our object creation code and passed a value to be set to color property.&lt;/p&gt;

&lt;p&gt;Remember, as we created a constructor to override the default php constructor, we need to pass a parameter to the constructor.&lt;/p&gt;

&lt;p&gt;If you try -- &lt;strong&gt;$bmw = new Car();&lt;/strong&gt;  it will give error because our constructor expects one parameter.&lt;/p&gt;

&lt;p&gt;Now we will create a destructor which will print message when object is being destroyed&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
class Car
{
    public $color = ‘red’;
    public $numberOfTires = 4;
    public function __construct($color)
    {
        $this-&amp;gt;color = $color;
    }
    Public function __destruct()
    {
        echo "Object is being destroyed";
    }
    public function setColor($val)
    {
        $this-&amp;gt;color = $val;
    }
    public function getColor()
    {
        return $this-&amp;gt;color;
    }
}
$bmw = new Car(“white”);
echo $bmw-&amp;gt;getColor(); //this will print “white” because our constructor assigned value “white” to color property
$bmw-&amp;gt;setColor(“black”); // this will overwrite color property to black
echo $bmw-&amp;gt;getColor();
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;The constructor method is a special magic method introduced with PHP 5 which allows to initialize object properties or perform some action at the time of object instantiation.&lt;/li&gt;
&lt;li&gt;The destructor method is a special magic method which allows to perform some actions at the time when object is being destroyed.&lt;/li&gt;
&lt;li&gt;Classes which have a constructor method, executes that automatically when an object is instantiated or created.&lt;/li&gt;
&lt;li&gt;Classes which have a destructor method, executes that automatically when an object is being destroyed.&lt;/li&gt;
&lt;li&gt;The constructor and destructor are not required. Every php class has a default constructor and destructor. If you create a constructor or destructor, the default one is ignored automatically.&lt;/li&gt;
&lt;li&gt;PHP only executes one constructor and one destructor ever.&lt;/li&gt;
&lt;li&gt;The “__construct” and “__destruct” method starts with two underscores.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Assigning visibility in PHP
&lt;/h2&gt;

&lt;p&gt;Previously we discussed to use one of the keywords – public, private and protected when defining property or a method. Public, Protected and Private are three types of visibility in PHP to control access of properties, variables or methods.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Public:&lt;/strong&gt; Public method or variable is accessible everywhere.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Private:&lt;/strong&gt; If property variable or method is private then it is accessible only from inside the class. For example: if color property of class Car is private then you cannot access it outside the class like – &lt;strong&gt;&lt;em&gt;echo $bmw-&amp;gt;color;&lt;/em&gt;&lt;/strong&gt; but a method like &lt;strong&gt;&lt;em&gt;getColor&lt;/em&gt;&lt;/strong&gt; can access it because it is inside the class.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Protected:&lt;/strong&gt; If property variable or method is protected then it is accessible from inside the class or from inside any class which is inherited from this class.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why we should not use public visibility all the time?&lt;/p&gt;

&lt;p&gt;Because it's not at all secure to keep everything public. To make it secure, PHP OOP has introduced these different visibilities.&lt;/p&gt;

&lt;p&gt;Let’s discuss an example showing the importance of private or protected visibility in PHP OOP:&lt;/p&gt;

&lt;p&gt;Make changes to our Car Class code 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;&amp;lt;?php
class Car
{
    private $color = ‘red’;
    private $numberOfTires = 4;
    public function __construct($color)
    {
        $this-&amp;gt;color = $color;
    }
    public function __destruct()
    {
        echo "Object is being destroyed";
    }
    public function setColor($val)
    {
        $this-&amp;gt;color = $val;
    }
    public function getColor()
    {
        return $this-&amp;gt;color;
    }
}
$bmw = new Car(“white”);
echo $bmw-&amp;gt;color; //this will give error because color property is private
echo $bmw-&amp;gt;getColor(); //this will print “white” because our constructor assigned value “white” to color property
$bmw-&amp;gt;setColor(“black”); // this will overwrite color property to black
echo $bmw-&amp;gt;getColor();
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As shown in above code, we made properties private to keep them hidden from the outer world. So, if someone tries to access color property using &lt;strong&gt;&lt;em&gt;$bmw-&amp;gt;color&lt;/em&gt;&lt;/strong&gt; , it will give error.&lt;/p&gt;

&lt;p&gt;And that’s why we created two methods &lt;strong&gt;getColor&lt;/strong&gt; and &lt;strong&gt;setColor&lt;/strong&gt; which are also known as getter and setter methods because they act as a intermediary between outer world and private variable.&lt;/p&gt;

&lt;p&gt;So if you want to get the value of color, use &lt;strong&gt;&lt;em&gt;$bmw-&amp;gt;getColor()&lt;/em&gt;&lt;/strong&gt; and if you want to set value of color , use &lt;strong&gt;&lt;em&gt;$bmw-&amp;gt;setColor(“white”)&lt;/em&gt;&lt;/strong&gt;. This is known as encapsulation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Static Methods and Properties
&lt;/h2&gt;

&lt;p&gt;A static method or property can be accessed without the need instantiating object. Just use class name, scope resolution operator and property or method name.&lt;/p&gt;

&lt;p&gt;We already learnt three modifiers – private, protected and public. Static is the fourth access modifier in PHP OOP which allows access to properties and methods without the need to create objects from the classes.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to create static method and property
&lt;/h3&gt;

&lt;p&gt;To define methods and properties as static, you need to use reserved &lt;strong&gt;static&lt;/strong&gt; keyword.&lt;/p&gt;

&lt;p&gt;Let’s see it in our example:&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
Class Car
{
    private $color = ‘red’;
    private $numberOfTires = 4;
    public static $country = “Canada”;
    public function __construct($color)
    {
        $this-&amp;gt;color = $color;
    }
    Public function __destruct()
    {
        echo "Object is being destroyed";
    }
    public function setColor($val)
    {
        $this-&amp;gt;color = $val;
    }
    Public function getColor()
    {
        return $this-&amp;gt;color;
    }
}
$bmw = new Car(“white”);
echo $bmw-&amp;gt;color; //this will give error because color property is private
echo $bmw-&amp;gt;getColor(); //this will print “white” because our constructor assigned value “white” to color property
$bmw-&amp;gt;setColor(“black”); // this will overwrite color property to black
echo $bmw-&amp;gt;getColor();
echo Car::$country;
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here I defined a constant name country with value Canada. Now I don’t need an object to access that constant.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;To use the static method and properties within the class, use &lt;strong&gt;self&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;PHP OOP brings lot of advantages compared to procedural programming and it provides easier approach to build complex web applications with easier scalability.&lt;/p&gt;

&lt;p&gt;Now, you should be comfortable with Object Oriented Programming style and you should have sound understanding of the basic concepts of PHP OOP i.e Object Oriented Programming.&lt;/p&gt;

&lt;p&gt;In the next parts, we will learn advanced concepts of PHP OOP, helpful to create highly modular and complex applications.&lt;/p&gt;

&lt;p&gt;Hope you liked this tutorial about OOP in PHP. Do you have any questions or confusion? Comment below.&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://www.parthpatel.net/php-oop-classes-objects/" rel="noopener noreferrer"&gt;Object Oriented Programming in PHP for Beginner Series&lt;/a&gt; appeared first on &lt;a href="https://www.parthpatel.net" rel="noopener noreferrer"&gt;Parth Patel - a Web Developer&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>php</category>
      <category>learnphpoop</category>
      <category>oopsconceptsinphp</category>
      <category>oopsinphp</category>
    </item>
  </channel>
</rss>
