<?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: Fouladgar.dev</title>
    <description>The latest articles on Forem by Fouladgar.dev (@mohammadfouladgar).</description>
    <link>https://forem.com/mohammadfouladgar</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%2F98553%2Fbce704b2-bea8-4e85-a00f-59084e2fc487.jpeg</url>
      <title>Forem: Fouladgar.dev</title>
      <link>https://forem.com/mohammadfouladgar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mohammadfouladgar"/>
    <language>en</language>
    <item>
      <title>Authentication with Laravel OTP</title>
      <dc:creator>Fouladgar.dev</dc:creator>
      <pubDate>Tue, 01 Mar 2022 12:29:10 +0000</pubDate>
      <link>https://forem.com/mohammadfouladgar/authentication-with-laravel-otp-58ha</link>
      <guid>https://forem.com/mohammadfouladgar/authentication-with-laravel-otp-58ha</guid>
      <description>&lt;p&gt;Nowadays most web applications prefer to use &lt;strong&gt;OTP&lt;/strong&gt;(One-Time Password) instead of using username/password which was a classic authentication system to validate users. Because, this way is more secure and in contrast to static passwords, they are not vulnerable to replay attacks.&lt;/p&gt;

&lt;p&gt;Maybe you are also worried about implementing a one-time password as a web developer in the &lt;strong&gt;Laravel&lt;/strong&gt; framework.&lt;/p&gt;

&lt;p&gt;There are many concerns about that such as :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How can I generate a secure token?&lt;/li&gt;
&lt;li&gt;Where should I store generated token?&lt;/li&gt;
&lt;li&gt;Is token valid or not? Is token expired or not?&lt;/li&gt;
&lt;li&gt;How can I have an integrated and also usable system for different user providers?&lt;/li&gt;
&lt;li&gt;etc…&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Well, good news! We have just released a package to resolve your concerns. Here you are. This is Laravel OTP package.&lt;/p&gt;

&lt;p&gt;Let’s start a practical implementation step by step.&lt;/p&gt;

&lt;h1&gt;
  
  
  Getting started
&lt;/h1&gt;

&lt;p&gt;First, you should install OTP package via composer:&lt;/p&gt;

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

composer require fouladgar/laravel-otp


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

&lt;/div&gt;
&lt;p&gt;Then, publish &lt;code&gt;config/otp.php&lt;/code&gt; file by running:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan vendor:publish --provider="Fouladgar\OTP\ServiceProvider" --tag="config"


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

&lt;/div&gt;
&lt;p&gt;And Finally migrate the database:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

php artisan migrate


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

&lt;/div&gt;
&lt;h1&gt;
  
  
  Model Preparation
&lt;/h1&gt;

&lt;p&gt;As next step, make sure the user model implement &lt;code&gt;Fouladgar\OTP\Contracts\OTPNotifiable&lt;/code&gt;, and also use &lt;code&gt;Fouladgar\OTP\Concerns\HasOTPNotify&lt;/code&gt; trait:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h1&gt;
  
  
  SMS Client
&lt;/h1&gt;

&lt;p&gt;There is a default &lt;code&gt;OTPSMSChannel&lt;/code&gt; which needs a SMS client for sending generated token to the user mobile phone. So, you should specify your SMS client and implement &lt;code&gt;Fouladgar\OTP\Contracts\SMSClient&lt;/code&gt; contract. This contract requires you to implement &lt;code&gt;sendMessage&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;This method will return your SMS service API results via a &lt;code&gt;Fouladgar\OTP\Notifications\Messages\MessagePayload&lt;/code&gt; object which contains user &lt;strong&gt;mobile&lt;/strong&gt; and &lt;strong&gt;token&lt;/strong&gt; message:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Next, you should set the client wrapper &lt;code&gt;SampleSMSClient&lt;/code&gt; class in &lt;code&gt;config/otp.php&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
It’s almost over…
&lt;h1&gt;
  
  
  Setup Routes and Controller
&lt;/h1&gt;

&lt;p&gt;We need some routes to &lt;strong&gt;send&lt;/strong&gt; and &lt;strong&gt;validate&lt;/strong&gt; the token. Let’s make them and implement our controller.&lt;/p&gt;

&lt;p&gt;You may add those in the &lt;code&gt;web&lt;/code&gt; or &lt;code&gt;api&lt;/code&gt; routes. It depends on you want to use OTP as &lt;strong&gt;Full Stack&lt;/strong&gt; or &lt;strong&gt;API Back-End&lt;/strong&gt;. It’s up to you. In this article I prefer use the second way.&lt;/p&gt;

&lt;p&gt;Well, open the &lt;code&gt;routes/api.php&lt;/code&gt; and put this routes:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And then create a AuthController.php class like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Finish. Now, you can call the routes like below:
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

// send otp request
curl --request POST \
  --url http://localhost/api/send-otp \
  --data '{
 "mobile" : "09389599530"
}'

// validate otp request
curl --request POST \
  --url http://localhost:8585/api/validate-otp \
  --data '{
 "mobile" : "09389599530",
 "token" : "94352"
}'


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

&lt;/div&gt;
&lt;p&gt;That’s it.&lt;/p&gt;

&lt;p&gt;For more details, please check out the documentation in GitHub:&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&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%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/mohammad-fouladgar" rel="noopener noreferrer"&gt;
        mohammad-fouladgar
      &lt;/a&gt; / &lt;a href="https://github.com/mohammad-fouladgar/laravel-otp" rel="noopener noreferrer"&gt;
        laravel-otp
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      This package provides convenient methods for sending and validating OTP notifications to users for authentication.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Laravel OTP(One-Time Password)&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a href="https://packagist.org/packages/fouladgar/laravel-otp" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/7e4e0f5ca14599ce9a681da88e49742e789021bf3182851987d16d118316d435/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f666f756c61646761722f6c61726176656c2d6f74702e737667" alt="Latest Version on Packagist"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/c72b3ab3fa5a523e3d7c99b546540364b5e43dacdd629f345e410ba71b4a726a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d6f68616d6d61642d666f756c61646761722f6c61726176656c2d6f74702f72756e2d74657374732e796d6c3f6c6162656c3d7465737473"&gt;&lt;img src="https://camo.githubusercontent.com/c72b3ab3fa5a523e3d7c99b546540364b5e43dacdd629f345e410ba71b4a726a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d6f68616d6d61642d666f756c61646761722f6c61726176656c2d6f74702f72756e2d74657374732e796d6c3f6c6162656c3d7465737473" alt="Test Status"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/d8c6141744516b8cf6580d5f3ff607cb7561e0233accf73103d95b80629785bc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d6f68616d6d61642d666f756c61646761722f6c61726176656c2d6f74702f7068702d63732d66697865722e796d6c3f6c6162656c3d636f64652532307374796c65"&gt;&lt;img src="https://camo.githubusercontent.com/d8c6141744516b8cf6580d5f3ff607cb7561e0233accf73103d95b80629785bc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d6f68616d6d61642d666f756c61646761722f6c61726176656c2d6f74702f7068702d63732d66697865722e796d6c3f6c6162656c3d636f64652532307374796c65" alt="Code Style Status"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/213be9a9a162c85760e868aa9378ac1bbc47ea85656e91976cdb1f92d4a6c541/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f666f756c61646761722f6c61726176656c2d6f7470"&gt;&lt;img src="https://camo.githubusercontent.com/213be9a9a162c85760e868aa9378ac1bbc47ea85656e91976cdb1f92d4a6c541/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f666f756c61646761722f6c61726176656c2d6f7470" alt="Total Downloads"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Introduction&lt;/h2&gt;
&lt;/div&gt;
&lt;p&gt;Most web applications need an OTP(one-time password) or secure code to validate their users. This package allows you to
send/resend and validate OTP for users authentication with user-friendly methods.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Version Compatibility&lt;/h2&gt;
&lt;/div&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Laravel&lt;/th&gt;
&lt;th&gt;Laravel-OTP&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;11.0.x&lt;/td&gt;
&lt;td&gt;4.2.x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10.0.x&lt;/td&gt;
&lt;td&gt;4.0.x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9.0.x&lt;/td&gt;
&lt;td&gt;3.0.x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6.0.x to 8.0.x&lt;/td&gt;
&lt;td&gt;1.0.x&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Basic Usage:&lt;/h2&gt;

&lt;/div&gt;
&lt;div class="highlight highlight-text-html-php notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;&lt;span class="pl-ent"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="pl-c"&gt;/*&lt;/span&gt;
&lt;span class="pl-c"&gt;|--------------------------------------------------------------------------&lt;/span&gt;
&lt;span class="pl-c"&gt;| Send OTP via SMS.&lt;/span&gt;
&lt;span class="pl-c"&gt;|--------------------------------------------------------------------------&lt;/span&gt;
&lt;span class="pl-c"&gt;*/&lt;/span&gt;
&lt;span class="pl-en"&gt;OTP&lt;/span&gt;()-&amp;gt;&lt;span class="pl-en"&gt;send&lt;/span&gt;(&lt;span class="pl-s"&gt;'&lt;span class="pl-s"&gt;+98900000000&lt;/span&gt;'&lt;/span&gt;); 
&lt;span class="pl-c"&gt;// Or&lt;/span&gt;
&lt;span class="pl-en"&gt;OTP&lt;/span&gt;(&lt;span class="pl-s"&gt;'&lt;span class="pl-s"&gt;+98900000000&lt;/span&gt;'&lt;/span&gt;);

&lt;span class="pl-c"&gt;/*&lt;/span&gt;
&lt;span class="pl-c"&gt;|--------------------------------------------------------------------------&lt;/span&gt;
&lt;span class="pl-c"&gt;| Send OTP via channels.&lt;/span&gt;
&lt;span class="pl-c"&gt;|--------------------------------------------------------------------------&lt;/span&gt;
&lt;span class="pl-c"&gt;*/&lt;/span&gt;
&lt;span class="pl-en"&gt;OTP&lt;/span&gt;()-&amp;gt;&lt;span class="pl-en"&gt;channel&lt;/span&gt;([&lt;span class="pl-s"&gt;'&lt;span class="pl-s"&gt;otp_sms&lt;/span&gt;'&lt;/span&gt;, &lt;span class="pl-s"&gt;'&lt;span class="pl-s"&gt;mail&lt;/span&gt;'&lt;/span&gt;, \&lt;span class="pl-v"&gt;App&lt;/span&gt;\&lt;span class="pl-v"&gt;Channels&lt;/span&gt;\&lt;span class="pl-v"&gt;CustomSMSChannel&lt;/span&gt;::class])
     -&amp;gt;&lt;span class="pl-en"&gt;send&lt;/span&gt;(&lt;span class="pl-s"&gt;'&lt;span class="pl-s"&gt;+98900000000&lt;/span&gt;'&lt;/span&gt;);
&lt;span class="pl-c"&gt;// Or&lt;/span&gt;
&lt;span class="pl-en"&gt;OTP&lt;/span&gt;(&lt;span class="pl-s"&gt;'&lt;span class="pl-s"&gt;+98900000000&lt;/span&gt;'&lt;/span&gt;, [&lt;span class="pl-s"&gt;'&lt;span class="pl-s"&gt;otp_sms&lt;/span&gt;'&lt;/span&gt;, &lt;span class="pl-s"&gt;'&lt;span class="pl-s"&gt;mail&lt;/span&gt;'&lt;/span&gt;, \&lt;span class="pl-v"&gt;App&lt;/span&gt;\&lt;span class="pl-v"&gt;Channels&lt;/span&gt;\&lt;span class="pl-v"&gt;CustomSMSChannel&lt;/span&gt;::class]);

&lt;span class="pl-c"&gt;/*&lt;/span&gt;
&lt;span class="pl-c"&gt;|--------------------------------------------------------------------------&lt;/span&gt;
&lt;span class="pl-c"&gt;| Send OTP for specific user provider&lt;/span&gt;
&lt;span class="pl-c"&gt;|--------------------------------------------------------------------------&lt;/span&gt;
&lt;span class="pl-c"&gt;*/&lt;/span&gt;
&lt;span class="pl-en"&gt;OTP&lt;/span&gt;()-&amp;gt;&lt;span class="pl-en"&gt;useProvider&lt;/span&gt;(&lt;span class="pl-s"&gt;'&lt;/span&gt;&lt;/pre&gt;…
&lt;/div&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/mohammad-fouladgar/laravel-otp" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Hope to useful this package. I’m waiting your opinions and comments.&lt;/p&gt;

&lt;p&gt;Thank you for sharing your valuable time with me.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>otp</category>
      <category>php</category>
      <category>authentication</category>
    </item>
    <item>
      <title>Laravel Mobile Verification</title>
      <dc:creator>Fouladgar.dev</dc:creator>
      <pubDate>Sat, 01 Feb 2020 12:19:37 +0000</pubDate>
      <link>https://forem.com/mohammadfouladgar/laravel-mobile-verification-388h</link>
      <guid>https://forem.com/mohammadfouladgar/laravel-mobile-verification-388h</guid>
      <description>&lt;p&gt;Many web applications require users to verify their mobile numbers before using the application. Rather than forcing you to re-implement this on each application, ‘Laravel Mobile Verification’ is a package that provides convenient methods and features for &lt;strong&gt;send&lt;/strong&gt;, &lt;strong&gt;verify&lt;/strong&gt; and &lt;strong&gt;resend&lt;/strong&gt; verification codes.&lt;/p&gt;

&lt;h1&gt;
  
  
  Basic Setup
&lt;/h1&gt;

&lt;p&gt;In the beginning, verify that your &lt;code&gt;User&lt;/code&gt; model implements the &lt;code&gt;MustVerifyMobile&lt;/code&gt; interface and use respected trait:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Next, you should specify your SMS service which any service (i.e. &lt;code&gt;Nexmo&lt;/code&gt;, &lt;code&gt;Twilio&lt;/code&gt;) are applicable. For sending SMS notifications via this package, you need to implement the &lt;code&gt;SMSClient&lt;/code&gt; interface. This interface requires you to implement &lt;code&gt;sendMessage&lt;/code&gt; method and this method will return your SMS service API result via a &lt;code&gt;Payload&lt;/code&gt; object which contains user &lt;strong&gt;mobile number&lt;/strong&gt; and &lt;strong&gt;token&lt;/strong&gt; message:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In order to set your SMS Client, you should publish the mobile_verifier.php config file with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan vendor:publish &lt;span class="nt"&gt;--provider&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Fouladgar&lt;/span&gt;&lt;span class="se"&gt;\M&lt;/span&gt;&lt;span class="s2"&gt;obileVerification&lt;/span&gt;&lt;span class="se"&gt;\S&lt;/span&gt;&lt;span class="s2"&gt;erviceProvider"&lt;/span&gt; &lt;span class="nt"&gt;--tag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"config"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;And set your client class:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h1&gt;
  
  
  Usage
&lt;/h1&gt;

&lt;p&gt;Here is how you can send a verification token after user registration:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h1&gt;
  
  
  Verify
&lt;/h1&gt;

&lt;p&gt;You should send &lt;code&gt;token&lt;/code&gt; message of an authenticated user to this route &lt;code&gt;/auth/mobile/verify&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; POST &lt;span class="se"&gt;\&lt;/span&gt;
      http://example.com/auth/mobile/verify &lt;span class="se"&gt;\&lt;/span&gt;
      &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s1"&gt;'Accept: application/json'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
      &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s1"&gt;'Authorization: YOUR_JWT_TOKEN'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
      &lt;span class="nt"&gt;-F&lt;/span&gt; &lt;span class="nv"&gt;token&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;YOUR_VERIFICATION_TOKEN
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h1&gt;
  
  
  Resend
&lt;/h1&gt;

&lt;p&gt;If you need to resend a verification token message, you can use this route &lt;code&gt;/auth/mobile/resend&lt;/code&gt; for an authenticated user:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; POST &lt;span class="se"&gt;\&lt;/span&gt;
      http://example.com/auth/mobile/resend &lt;span class="se"&gt;\&lt;/span&gt;
      &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s1"&gt;'Accept: application/json'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
      &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s1"&gt;'Authorization: YOUR_JWT_TOKEN'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;For more details, please check out the documentation:&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qF2jUiUG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-6a5bca60a4ebf959a6df7f08217acd07ac2bc285164fae041eacb8a148b1bab9.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/mohammad-fouladgar"&gt;
        mohammad-fouladgar
      &lt;/a&gt; / &lt;a href="https://github.com/mohammad-fouladgar/laravel-mobile-verification"&gt;
        laravel-mobile-verification
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      This package provides convenient methods for sending and verifying mobile verification requests.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
Laravel Mobile Verification&lt;/h1&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer" href="https://raw.githubusercontent.com/mohammad-fouladgar/laravel-mobile-verification/master/./cover.jpg"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n9nYvUaH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/mohammad-fouladgar/laravel-mobile-verification/master/./cover.jpg" alt="alt text" title="EloquentBuilder"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://travis-ci.org/mohammad-fouladgar/laravel-mobile-verification" rel="nofollow"&gt;&lt;img src="https://camo.githubusercontent.com/5ea9fdbc95fcd701aa03fcd6970932529648979f/68747470733a2f2f7472617669732d63692e6f72672f6d6f68616d6d61642d666f756c61646761722f6c61726176656c2d6d6f62696c652d766572696669636174696f6e2e7376673f6272616e63683d6d6173746572" alt="Build Status"&gt;&lt;/a&gt;
&lt;a href="https://coveralls.io/github/mohammad-fouladgar/laravel-mobile-verification" rel="nofollow"&gt;&lt;img src="https://camo.githubusercontent.com/8ec7688737a8c3267643b55710d8b334f2ec54ff/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d6f68616d6d61642d666f756c61646761722f6c61726176656c2d6d6f62696c652d766572696669636174696f6e2f62616467652e737667" alt="Coverage Status"&gt;&lt;/a&gt;
&lt;a href="https://scrutinizer-ci.com/g/mohammad-fouladgar/laravel-mobile-verification" rel="nofollow"&gt;&lt;img src="https://camo.githubusercontent.com/8c98871e6e66a7587d0d2c1e99e65a46f1059abf/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6d6f68616d6d61642d666f756c61646761722f6c61726176656c2d6d6f62696c652d766572696669636174696f6e2e7376673f7374796c653d666c61742d737175617265" alt="Quality Score"&gt;&lt;/a&gt;
&lt;a href="https://packagist.org/packages/fouladgar/laravel-mobile-verification" rel="nofollow"&gt;&lt;img src="https://camo.githubusercontent.com/b750a5a963c8307c0381ef49b35610bcced1a0da/68747470733a2f2f706f7365722e707567782e6f72672f666f756c61646761722f6c61726176656c2d6d6f62696c652d766572696669636174696f6e2f762f737461626c65" alt="Latest Stable Version"&gt;&lt;/a&gt;
&lt;a href="https://packagist.org/packages/fouladgar/laravel-mobile-verification" rel="nofollow"&gt;&lt;img src="https://camo.githubusercontent.com/fd7e071fab8812cf3cb8d4fd09ce347770fce678/68747470733a2f2f706f7365722e707567782e6f72672f666f756c61646761722f6c61726176656c2d6d6f62696c652d766572696669636174696f6e2f646f776e6c6f616473" alt="Total Downloads"&gt;&lt;/a&gt;
&lt;a href="https://packagist.org/packages/fouladgar/laravel-mobile-verification" rel="nofollow"&gt;&lt;img src="https://camo.githubusercontent.com/3cf7fa9c53b8bedf4fcd151625650777bf6bc31e/68747470733a2f2f706f7365722e707567782e6f72672f666f756c61646761722f6c61726176656c2d6d6f62696c652d766572696669636174696f6e2f6c6963656e7365" alt="License"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
Introduction&lt;/h2&gt;
&lt;p&gt;Many web applications require users to verify their mobile numbers before using the application. Rather than forcing you to re-implement this on each application, this package provides convenient methods for sending and verifying mobile verification requests.&lt;/p&gt;
&lt;h2&gt;
Installation&lt;/h2&gt;
&lt;p&gt;You can install the package via composer:&lt;/p&gt;
&lt;div class="highlight highlight-source-shell"&gt;&lt;pre&gt;composer require fouladgar/laravel-mobile-verification&lt;/pre&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;p&gt;Laravel 5.5 uses Package Auto-Discovery, so you are not required to add ServiceProvider manually.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
Laravel &amp;lt;= 5.4.x&lt;/h3&gt;
&lt;p&gt;If you don't use Auto-Discovery, add the ServiceProvider to the providers array in &lt;code&gt;config/app.php&lt;/code&gt; file&lt;/p&gt;
&lt;div class="highlight highlight-text-html-php"&gt;&lt;pre&gt;&lt;span class="pl-s1"&gt;&lt;span class="pl-s"&gt;&lt;span class="pl-pds"&gt;'&lt;/span&gt;providers&lt;span class="pl-pds"&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class="pl-k"&gt;=&amp;gt;&lt;/span&gt; [&lt;/span&gt;
&lt;span class="pl-s1"&gt;  &lt;span class="pl-c"&gt;&lt;span class="pl-c"&gt;/*&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="pl-s1"&gt;&lt;span class="pl-c"&gt;   * Package Service Providers...&lt;/span&gt;&lt;/span&gt;
&lt;span class="pl-s1"&gt;&lt;span class="pl-c"&gt;   &lt;span class="pl-c"&gt;*/&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="pl-s1"&gt;  &lt;span class="pl-c1"&gt;Fouladgar\MobileVerification\&lt;/span&gt;&lt;span class="pl-c1"&gt;ServiceProvider&lt;/span&gt;&lt;span class="pl-k"&gt;::&lt;/span&gt;&lt;span class="pl-c1"&gt;class&lt;/span&gt;,&lt;/span&gt;
&lt;span class="pl-s1"&gt;],&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
Configuration&lt;/h2&gt;
&lt;p&gt;To get started, you should publish the &lt;code&gt;config/mobile_verification.php&lt;/code&gt; config file with:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;php artisan vendor:publish --provider="Fouladgar\MobileVerification\ServiceProvider" --tag="config"
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you’re using another table name for &lt;code&gt;users&lt;/code&gt; table or different column name for &lt;code&gt;mobile&lt;/code&gt; or even &lt;code&gt;mobile_verification_tokens&lt;/code&gt; table, you can customize their values in config file:&lt;/p&gt;
&lt;div class="highlight highlight-text-html-php"&gt;
&lt;pre&gt;&lt;span class="pl-s1"&gt;&lt;span class="pl-c"&gt;&lt;span class="pl-c"&gt;//&lt;/span&gt; config/mobile_verification.php&lt;/span&gt;&lt;/span&gt;
&lt;span class="pl-s1"&gt;&lt;span class="pl-k"&gt;&amp;lt;&lt;/span&gt;?&lt;span class="pl-c1"&gt;php&lt;/span&gt;&lt;/span&gt;
&lt;span class="pl-s1"&gt;&lt;span class="pl-k"&gt;return&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;…&lt;/div&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/mohammad-fouladgar/laravel-mobile-verification"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;



</description>
      <category>laravelmobileverification</category>
      <category>mobileconfirmation</category>
      <category>laravel</category>
      <category>php</category>
    </item>
    <item>
      <title>Laravel Eloquent Filter (All in one)</title>
      <dc:creator>Fouladgar.dev</dc:creator>
      <pubDate>Wed, 01 Jan 2020 08:48:14 +0000</pubDate>
      <link>https://forem.com/mohammadfouladgar/laravel-eloquent-filter-all-in-one-3lal</link>
      <guid>https://forem.com/mohammadfouladgar/laravel-eloquent-filter-all-in-one-3lal</guid>
      <description>&lt;p&gt;Have you ever stuck in a situation which a lot of parameters should be send to your API? and you must create tons of queries based on them? If your answer is yes, do not stop reading!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First&lt;/strong&gt; of all, if you haven’t read &lt;strong&gt;&lt;a href="https://dev.to/mohammadfouladgar/making-the-advanced-search-query-with-eloquent-builder-in-laravel-30g3"&gt;This&lt;/a&gt;&lt;/strong&gt; article about Eloquent filters before, I strongly advise you to take a look at it. In the mentioned article you will learn how to make advance query filters for your Eloquent model.&lt;/p&gt;

&lt;p&gt;Also, You may need a secure filter. Secure filters will authorize the user for applying a given filter, which you can find the related article &lt;strong&gt;&lt;a href="https://dev.to/mohammadfouladgar/laravel-making-filter-for-model-with-checking-authorization-filter-2af"&gt;here&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now, what if your Laravel project is not following default directory structure?&lt;/p&gt;

&lt;h2&gt;
  
  
  Define Filters Per Domain/Module
&lt;/h2&gt;

&lt;p&gt;In a small Laravel project, you can use the default architecture (MVC) and its respective directory structure as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── app
│   ├── Console
├── EloquentFilters // Default directory for filters
│   └── Product
│       ├── PriceMoreThanFilter.php
│   └── User
│       ├── AgeMoreThanFilter.php
│       └── GenderFilter.php
└── Exceptions
│   ├── Http
│   │   ├── Controllers
│   │   ├── Middleware
│   │   └── Requests
│   └── Providers
├── bootstrap
├── config
├── database
├── public
├── resources
├── routes
├── storage
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;And you can using &lt;strong&gt;EloquentBuilder&lt;/strong&gt; like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;But&lt;/strong&gt; when your project grows by the time, the default structure is not enough. Eventually, this problem leads you to use a different architecture (i.e. &lt;a href="https://en.wikipedia.org/wiki/Domain-driven_design" rel="noopener noreferrer"&gt;Domain-Driven-Desgin&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/Hierarchical_model%E2%80%93view%E2%80%93controller" rel="noopener noreferrer"&gt;HMVC&lt;/a&gt;, …) or modifying current one. Therefore changing the directory structure in inevitable. Take a look at this one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── app
├── bootstrap
├── config
├── database
├── Domains
│   ├── Product
│   │   ├── database
│   │   │   └── migrations
│   │   ├── src
│   │       ├── Filters //Custom directory for Product model filters
│   │       │   └── PriceMoreThanFilter.php
│   │       ├── Entities
│   │       ├── Http
│   │          └── Controllers
│   │       ├── routes
│   │       └── Services
│   ├── User
│   │   ├── database
│   │   │   └── migrations
│   │   ├── src
│   │       ├── Filters //Custom directory for User model filters
│   │       │   └── AgeMoreThanFilter.php
│   │       ├── Entities
│   │       ├── Http
│   │          └── Controllers
│   │       ├── routes
│   │       └── Services
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In this week, EloquentBuilder &lt;a href="https://github.com/mohammad-fouladgar/eloquent-builder/tree/v2.0.0" rel="noopener noreferrer"&gt;v2.0&lt;/a&gt; has been released. In this version a new &lt;code&gt;setFilterNamespace&lt;/code&gt; method added to the package to let you create custom filters “per domain/module” by setting filters namespace of the fly.&lt;/p&gt;

&lt;p&gt;Now we can use filters for each model as below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



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

&lt;p&gt;Dealing with API incoming parameters is a tedious job, so getting help from a black box package helps you to handle them without any concern and complexity. Furthermore, now there is a possibility to use this advantage in larger projects with different structures.&lt;/p&gt;

&lt;p&gt;Special thanks to &lt;a href="https://twitter.com/50bhan" rel="noopener noreferrer"&gt;50bhan&lt;/a&gt; for this awesome PR:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_github-liquid-tag"&gt;
  &lt;h1&gt;
    &lt;a href="https://github.com/mohammad-fouladgar/eloquent-builder/pull/53" rel="noopener noreferrer"&gt;
      &lt;img class="github-logo" alt="GitHub logo" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg"&gt;
      &lt;span class="issue-title"&gt;
        Add domain/module support
      &lt;/span&gt;
      &lt;span class="issue-number"&gt;#53&lt;/span&gt;
    &lt;/a&gt;
  &lt;/h1&gt;
  &lt;div class="github-thread"&gt;
    &lt;div class="timeline-comment-header"&gt;
      &lt;a href="https://github.com/50bhan" rel="noopener noreferrer"&gt;
        &lt;img class="github-liquid-tag-img" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Favatars2.githubusercontent.com%2Fu%2F36772278%3Fv%3D4" alt="50bhan avatar"&gt;
      &lt;/a&gt;
      &lt;div class="timeline-comment-header-text"&gt;
        &lt;strong&gt;
          &lt;a href="https://github.com/50bhan" rel="noopener noreferrer"&gt;50bhan&lt;/a&gt;
        &lt;/strong&gt; posted on &lt;a href="https://github.com/mohammad-fouladgar/eloquent-builder/pull/53" rel="noopener noreferrer"&gt;&lt;time&gt;Dec 29, 2019&lt;/time&gt;&lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag-github-body"&gt;
      &lt;p&gt;With this feature, now we can how multiple filters in multiple directories to support domain/module directory structure. For detailed information, please read the documentation (&lt;strong&gt;customize-per-domain/module&lt;/strong&gt; section).&lt;/p&gt;
&lt;p&gt;This PR will closed issue #51&lt;/p&gt;

    &lt;/div&gt;
    &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/mohammad-fouladgar/eloquent-builder/pull/53" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>laravel</category>
      <category>php</category>
      <category>eloquent</category>
      <category>filter</category>
    </item>
    <item>
      <title>Laravel: Making secure filters for Eloquent Models</title>
      <dc:creator>Fouladgar.dev</dc:creator>
      <pubDate>Mon, 22 Jul 2019 15:15:40 +0000</pubDate>
      <link>https://forem.com/mohammadfouladgar/laravel-making-filter-for-model-with-checking-authorization-filter-2af</link>
      <guid>https://forem.com/mohammadfouladgar/laravel-making-filter-for-model-with-checking-authorization-filter-2af</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In the previous post, I introduced a package that allowed you to filter for your Eloquent models in Laravel.As you can see &lt;a href="https://dev.to/mohammadfouladgar/making-the-advanced-search-query-with-eloquent-builder-in-laravel-30g3"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The latest version of this package is available in Github repository: &lt;a href="https://github.com/mohammad-fouladgar/eloquent-builder"&gt;v1.0.1&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As I explained earlier,you may need to filter a model in your project like &lt;code&gt;User&lt;/code&gt; model.For example, filtering users based on gender, age, online / offline status and etc... .&lt;/p&gt;

&lt;p&gt;Now, you may check if the authenticated user actually has the authority to apply a given filter. For example, you may determine if a user has a premium account or is admin, can apply the &lt;code&gt;StatusFilter&lt;/code&gt; to get listing the online or offline people.&lt;/p&gt;

&lt;p&gt;Let's have a practical example below for a better understanding.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup EloquentBuilder package
&lt;/h2&gt;

&lt;p&gt;In the first, if you have not Laravel,run below command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;composer create-project &lt;span class="nt"&gt;--prefer-dist&lt;/span&gt; laravel/laravel authorize-filter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now go to root of the your application and install EloquentBuilder:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;authorize-filter
&lt;span class="nv"&gt;$ &lt;/span&gt;composer require mohammad-fouladgar/eloquent-builder
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now, you should create a new directory in the ‍‍&lt;code&gt;app&lt;/code&gt; directory as &lt;code&gt;EloquentFilters&lt;/code&gt;.This directory is the storage place of the model filters.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create Route and Controller
&lt;/h2&gt;

&lt;p&gt;In this step, we need to create a Route for users listing.So open your &lt;code&gt;routes/web.php&lt;/code&gt; file and add following route:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Route&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'users'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'UserController@index'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Then,you should create a controller named &lt;code&gt;UserController&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Now you can open bellow url on your browser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;http://localhost:8000/users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If you running this URL, users listing will be returned.&lt;br&gt;
In the next step, we will implement the &lt;code&gt;gender&lt;/code&gt; and &lt;code&gt;status&lt;/code&gt; filters.&lt;/p&gt;
&lt;h2&gt;
  
  
  Gender Filter
&lt;/h2&gt;

&lt;p&gt;At this point, we want to filter users based on gender.&lt;br&gt;
First of all, you should create a new directory as &lt;code&gt;User&lt;/code&gt; inside &lt;code&gt;EloquentFilters&lt;/code&gt;.This directory contains user model filters.&lt;br&gt;
Next, create a new file named &lt;code&gt;GenderFilter.php&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;app/EloquentFilters/User/GenderFilter.php&lt;/em&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;after creating above filter, open your browser and run this url for apply filter on your query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;http://localhost:8000/users?filter[gender]&lt;span class="o"&gt;=&lt;/span&gt;male
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;By running this url,users are listed who have the gender as male.&lt;/p&gt;
&lt;h2&gt;
  
  
  StatusFilter
&lt;/h2&gt;

&lt;p&gt;This filter has a &lt;code&gt;authorize&lt;/code&gt; method.within method we want to check if a user has a premium account, can apply the &lt;code&gt;StatusFilter&lt;/code&gt; to get listing the online or offline people.&lt;/p&gt;

&lt;p&gt;Take a look at the following filter:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;app/EloquentFilters/User/StatusFilter.php&lt;/em&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;For apply the above filter,in browser open the below url:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;http://localhost:8000/users?filter[gender]&lt;span class="o"&gt;=&lt;/span&gt;male&amp;amp;filter[status]&lt;span class="o"&gt;=&lt;/span&gt;online
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;For see more details click &lt;a href="https://github.com/mohammad-fouladgar/eloquent-builder#authorizing-filter"&gt;here&lt;/a&gt; please.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;In this article, we learned how to use the &lt;strong&gt;EloquentBuilder&lt;/strong&gt; package in the Laravel framework.&lt;/p&gt;

&lt;p&gt;Also learned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to make a filter for a model&lt;/li&gt;
&lt;li&gt;How to apply filters on query builder&lt;/li&gt;
&lt;li&gt;How to check the authorization to apply a given filter&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And finally having a clean code, readable and extensible.&lt;/p&gt;

&lt;p&gt;I hope this article will be of interest to you. I'm glad to have your valuable comments.&lt;/p&gt;

&lt;p&gt;Good luck...&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>eloquent</category>
      <category>filter</category>
    </item>
    <item>
      <title>Making the advanced search query with Eloquent-Builder in Laravel</title>
      <dc:creator>Fouladgar.dev</dc:creator>
      <pubDate>Wed, 05 Sep 2018 09:39:04 +0000</pubDate>
      <link>https://forem.com/mohammadfouladgar/making-the-advanced-search-query-with-eloquent-builder-in-laravel-30g3</link>
      <guid>https://forem.com/mohammadfouladgar/making-the-advanced-search-query-with-eloquent-builder-in-laravel-30g3</guid>
      <description>&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%2Fmljp8vfjfxveyy7i9368.jpg" 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%2Fmljp8vfjfxveyy7i9368.jpg" alt="Unsplash @aginsbrook" width="800" height="533"&gt;&lt;/a&gt;&lt;br&gt;
We needed an advanced search system in the recent project. This system included many filters that required a flexible and scalable search system.&lt;br&gt;
I decided to implement a package for this system that you can see it in the &lt;a href="https://github.com/mohammad-fouladgar/eloquent-builder" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; and use it in your project.&lt;/p&gt;
&lt;h2&gt;
  
  
  What's the problem?
&lt;/h2&gt;

&lt;p&gt;The problem is that you will encounter with a set of filters that you must check for a lot of conditions to add to the query.&lt;/p&gt;

&lt;p&gt;Writing a lot of terms will surely reduce the readability of your code and slow down the development process.&lt;/p&gt;

&lt;p&gt;Also, you can only use the filters and terms in the same scope and they will not be reusable.&lt;/p&gt;
&lt;h2&gt;
  
  
  But the Solution
&lt;/h2&gt;

&lt;p&gt;You must &lt;strong&gt;Refactor&lt;/strong&gt; your code!&lt;/p&gt;

&lt;p&gt;To solve this problem, you need to &lt;strong&gt;Refactor&lt;/strong&gt; your code by replacing many of your conditionals with &lt;strong&gt;Polymorphism&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Click &lt;a href="https://sourcemaking.com/refactoring/replace-conditional-with-polymorphism" rel="noopener noreferrer"&gt;here&lt;/a&gt; to learn more about this design pattern.&lt;/p&gt;
&lt;h2&gt;
  
  
  Practical Example:
&lt;/h2&gt;

&lt;p&gt;Suppose we want to get the list of the users with the requested parameters as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;http://dev.to/api/users/search?age_more_than&lt;span class="o"&gt;=&lt;/span&gt;25&amp;amp;gender&lt;span class="o"&gt;=&lt;/span&gt;male&amp;amp;has_published_post&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The Requested parameter will be as follows:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt; 
  &lt;span class="s1"&gt;'age_more_than'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'25'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s1"&gt;'gender'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'male'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s1"&gt;'has_published_post'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'true'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In a &lt;strong&gt;common&lt;/strong&gt; implementation, following code will be expected:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;We check out a condition for each request parameter.&lt;/p&gt;

&lt;p&gt;In the future, we will have more parameters that should be checked and applied in the code above that causes us to have a dirty code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use the Eloquent-Builder
&lt;/h2&gt;

&lt;p&gt;After &lt;a href="https://github.com/mohammad-fouladgar/eloquent-builder#installation" rel="noopener noreferrer"&gt;installing&lt;/a&gt; the package presented,change your code as follows:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You just send the &lt;strong&gt;model&lt;/strong&gt; and &lt;strong&gt;parameters&lt;/strong&gt; to &lt;strong&gt;‘to’&lt;/strong&gt; method.Then, you need to define the filter for each parameter that you want to add to the query.&lt;/p&gt;

&lt;p&gt;So easy!&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining a filter
&lt;/h2&gt;

&lt;p&gt;For example, I’ll implement one of the above example filters. Follow the example below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
For more details check out the &lt;a href="https://github.com/mohammad-fouladgar/eloquent-builder" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; repository.


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/mohammad-fouladgar" rel="noopener noreferrer"&gt;
        mohammad-fouladgar
      &lt;/a&gt; / &lt;a href="https://github.com/mohammad-fouladgar/eloquent-builder" rel="noopener noreferrer"&gt;
        eloquent-builder
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      This package provides an advanced filter for Laravel model based on incoming requets.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Provides a Eloquent query builder for Laravel&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer" href="https://github.com/mohammad-fouladgar/eloquent-builder./cover.jpg"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fmohammad-fouladgar%2Feloquent-builder.%2Fcover.jpg" alt="alt text" title="EloquentBuilder"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://packagist.org/packages/mohammad-fouladgar/eloquent-builder" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/63e2db0fd914bba043512be1a8ef64444a402f5088d2ac073259947bc633a58a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6f68616d6d61642d666f756c61646761722f656c6f7175656e742d6275696c6465722e737667" alt="Latest Version on Packagist"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/567fca3c7cb4bac9aeac4bbb26d424a3356f1beeb1ddd8ef99c97070f1611774/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d6f68616d6d61642d666f756c61646761722f656c6f7175656e742d6275696c6465722f72756e2d74657374732e796d6c3f6c6162656c3d7465737473"&gt;&lt;img src="https://camo.githubusercontent.com/567fca3c7cb4bac9aeac4bbb26d424a3356f1beeb1ddd8ef99c97070f1611774/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d6f68616d6d61642d666f756c61646761722f656c6f7175656e742d6275696c6465722f72756e2d74657374732e796d6c3f6c6162656c3d7465737473" alt="Test Status"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/89b5298261afa7ad29b173d47c8493ae7b1c9bedf0da2705a73af9c6b91d6572/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d6f68616d6d61642d666f756c61646761722f656c6f7175656e742d6275696c6465722f7068702d63732d66697865722e796d6c3f6c6162656c3d636f64652532307374796c65"&gt;&lt;img src="https://camo.githubusercontent.com/89b5298261afa7ad29b173d47c8493ae7b1c9bedf0da2705a73af9c6b91d6572/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d6f68616d6d61642d666f756c61646761722f656c6f7175656e742d6275696c6465722f7068702d63732d66697865722e796d6c3f6c6162656c3d636f64652532307374796c65" alt="Code Style Status"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/bbe340fe85033f65c2e8f892776abffb6cedf4fc3bdafedcabed97542dbf7996/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6f68616d6d61642d666f756c61646761722f656c6f7175656e742d6275696c646572"&gt;&lt;img src="https://camo.githubusercontent.com/bbe340fe85033f65c2e8f892776abffb6cedf4fc3bdafedcabed97542dbf7996/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6f68616d6d61642d666f756c61646761722f656c6f7175656e742d6275696c646572" alt="Total Downloads"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This package allows you to build eloquent queries, based on incoming request parameters. It greatly reduces the complexity of the
queries and conditions, which will make your code clean and maintainable.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Version Compatibility&lt;/h2&gt;
&lt;/div&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Laravel&lt;/th&gt;
&lt;th&gt;EloquentBuilder&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;11.0.x to 12.0.x&lt;/td&gt;
&lt;td&gt;5.x.x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10.0.x&lt;/td&gt;
&lt;td&gt;4.2.x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9.0.x&lt;/td&gt;
&lt;td&gt;4.0.x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6.0.x to 8.0.x&lt;/td&gt;
&lt;td&gt;3.0.x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5.0.x&lt;/td&gt;
&lt;td&gt;2.2.2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Basic Usage&lt;/h2&gt;
&lt;/div&gt;
&lt;p&gt;Suppose you want to get the list of the users with the requested parameters as follows:&lt;/p&gt;
&lt;div class="highlight highlight-text-html-php notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;&lt;span class="pl-c"&gt;//Get api/user/search?age_more_than=25&amp;amp;gender=male&amp;amp;has_published_post=true&lt;/span&gt;
[
    &lt;span class="pl-s"&gt;'&lt;span class="pl-s"&gt;age_more_than&lt;/span&gt;'&lt;/span&gt;  =&amp;gt; &lt;span class="pl-s"&gt;'&lt;span class="pl-s"&gt;25&lt;/span&gt;'&lt;/span&gt;,
    &lt;span class="pl-s"&gt;'&lt;span class="pl-s"&gt;gender&lt;/span&gt;'&lt;/span&gt;         =&amp;gt; &lt;span class="pl-s"&gt;'&lt;span class="pl-s"&gt;male&lt;/span&gt;'&lt;/span&gt;,
    &lt;span class="pl-s"&gt;'&lt;span class="pl-s"&gt;has_published_post&lt;/span&gt;'&lt;/span&gt; =&amp;gt; &lt;span class="pl-c1"&gt;true&lt;/span&gt;,
]&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;In a &lt;strong&gt;common&lt;/strong&gt; implementation, following code will be expected:&lt;/p&gt;
&lt;div class="highlight highlight-text-html-php notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;&lt;span class="pl-ent"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="pl-k"&gt;namespace&lt;/span&gt; &lt;span class="pl-v"&gt;App&lt;/span&gt;\&lt;span class="pl-v"&gt;Http&lt;/span&gt;\&lt;span class="pl-v"&gt;Controllers&lt;/span&gt;;

&lt;span class="pl-k"&gt;use&lt;/span&gt; &lt;span class="pl-v"&gt;App&lt;/span&gt;\&lt;span class="pl-v"&gt;Models&lt;/span&gt;\&lt;span class="pl-smi"&gt;User&lt;/span&gt;;
&lt;span class="pl-k"&gt;use&lt;/span&gt; &lt;span class="pl-v"&gt;Illuminate&lt;/span&gt;\&lt;span class="pl-v"&gt;Http&lt;/span&gt;\&lt;span class="pl-smi"&gt;Request&lt;/span&gt;;

&lt;span class="pl-k"&gt;class&lt;/span&gt; UserController &lt;span class="pl-k"&gt;extends&lt;/span&gt; Controller
{
    &lt;span class="pl-k"&gt;public&lt;/span&gt; &lt;span class="pl-k"&gt;function&lt;/span&gt; &lt;span class="pl-en"&gt;index&lt;/span&gt;(&lt;span class="pl-smi"&gt;&lt;span class="pl-smi"&gt;Request&lt;/span&gt;&lt;/span&gt; &lt;span class="pl-s1"&gt;&lt;span class="pl-c1"&gt;$&lt;/span&gt;request&lt;/span&gt;)
    {
        &lt;span class="pl-s1"&gt;&lt;span class="pl-c1"&gt;$&lt;/span&gt;users&lt;/span&gt; = User::&lt;span class="pl-en"&gt;where&lt;/span&gt;&lt;/pre&gt;…
&lt;/div&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/mohammad-fouladgar/eloquent-builder" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Good luck and thank you for sharing your valuable time with me.&lt;/p&gt;

&lt;p&gt;Happy Coding! &lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>eloquent</category>
      <category>filter</category>
    </item>
  </channel>
</rss>
