Laravel Validation timezone Example

Laravel Validation timezone Example

Hi Guys,

Today, I will learn you to create validation timezone in laravel.we will show example of laravel validation timezone.The field under validation must be a valid timezone identifier according to the timezone_identifiers_list PHP function.

Here, I will give you full example for simply timezone validation in laravel bellow.

solution
$request->validate([
  'time_zone' => 'timezone',
]);
Route : routes/web.php
Route::get('form/create','FormController@index');
Route::post('form/store','FormController@store')->name('form.store');
Controller : app/Http/Controllers/FormController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Blade;
use App\Models\User;
use App\Models\Post;

class FormController extends Controller
{   
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function create()
    {
        return view('form');
    }
    
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function store(Request $request)
    {
        $request->validate([
          'time_zone' => 'timezone'
        ]);
      dd('done');
    }
}
View : resources/views/form.php
<!DOCTYPE html>
<html>
<head>
  <title>From</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha512-MoRNloxbStBcD8z3M/2BmnT+rg4IsMxPkXaGh2zD6LGNNFE80W3onsAhRcMAMrSoyWL9xD7Ert0men7vR8LUZg==" crossorigin="anonymous" />
  <scrtimezonet src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></scrtimezonet>
  <scrtimezonet src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></scrtimezonet>
</head>
<body class="bg-dark">
  <div class="container">
    <div class="row">
      <div class="col-md-6 offset-3">
        <div class="card mt-5">
          <div class="card-header">
            <div class="row">
              <div class="col-md-9">
                 Laravel Validation timezone Example
              </div>
              <div class="col-md-3 text-right">
                <a href="{{ route('form') }}" class="btn btn-sm btn-outline-primary">Back</a>
              </div>
            </div>
          </div>
          <div class="card-body">
            @if (count($errors) > 0)
                  <div class="row">
                      <div class="col-md-12">
                          <div class="alert alert-danger alert-dismissible">
                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                              @foreach($errors->all() as $error)
                              {{ $error }} <br>
                              @endforeach      
                          </div>
                      </div>
                  </div>
                @endif
              <form action="{{ route('from.store') }}" method="post">
                @csrf
                <div class="row">
                  <div class="col-md-12">
                    <div class="form-group">
                      <label>Your Timezone:</label>
                      <input name="time_zone" type="text" class="form-control">
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-12">
                    <button class="btn btn-block btn-success">Submit</button>
                  </div>
                </div>
              </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>
</html>

It will help you...

Laravel Custom Middleware Tutorial Example

Laravel Custom Middleware Tutorial Example

Hi guys,

In this blog,I can create custom middleware in laravel, we are able to discover ways to create custom middleware in laravel utility and the way to use middleware in laravel based mission. truely create one custom middleware and take a look at language in query string.

clearly laravel middleware filter out all of the http request in laravel based totally tasks. as an instance when user is do any request that point middleware take a look at person is loggedin or now not and redirect therefore. Any person is not loggedin but he need to get entry to to dashboard or different things in projects that point middleware filter request redirect to consumer.

on this laravel middleware educational, we can supply a example of active or inactive users. Now we can name middleware in routes to restriction logged person to get right of entry to that routes, if he/she is blocked by way of admin.

In this laravel middleware tutorial, we will give a example of active or inactive users. Now we will call middleware in routes to restrict logged user to access that routes, if he/she is blocked by admin.

Step:1 Create Middleware

In this step, We have to create custom middleware in laravel based project. So let’s open your command prompt and run below command :

php artisan make:middleware CheckStatus
Step:2 Register Middleware

After successfully create middleware, go to app/http/kernel.php and register your custom middleware here :

app/Http/Kernel.php
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    ....
 
    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        ....
        'checkStatus' => \App\Http\Middleware\CheckStatus::class,
    ];
}
?>
Step:3 Implement logic In Middleware

After successfully register your middleware in laravel project, go to app/http/middleware and implement your logic here :

app/Http/Middleware/CheckStatus.php
<?php
namespace App\Http\Middleware;
use Closure;

class CheckStatus
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (auth()->user()->status == 'active') {
            return $next($request);
        }
            return response()->json('Your account is inactive');
 
    }
}
?>
Step:4 Create Route

In this step, simply we will create a route and use custom middleware here. Filter http every request and protect routes :

<?php
    use Illuminate\Support\Facades\Route;
    use App\Http\Controllers\HomeController;

    /*
    |--------------------------------------------------------------------------
    | Web Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | contains the "web" middleware group. Now create something great!
    |
    */
 
    Route::middleware(['checkStatus'])->group(function(){
    Route::get('home', 'HomeController@home');
});
Step:5 Add Method in Controller

Now we will create one method name language and let’s check :

app/Http/Controllers/HomeController.php
<?php
 namespace App\Http\Controllers;
 use Illuminate\Http\Request;
 
 class HomeController extends Controller
 {
     public function home()
     {
         dd('You are active');
     }
 }
?>

We have successfully create custom middleware in laravel based project with simple example. run the example.

It Will help you....