Laravel: Query Between 2 Dates

Getting Started

First, we're going to create a new project and I'm gonna name it wherebetween and add it to localhost with the name wherebetween.dev. If you have no idea on how to do this, please refer to my tutorial Installing Laravel - PHP Framework.

Setting up our Database

1. Open your phpMyAdmin and create a new database. In my case, I've created a database named post. 2. In our project, open .env file and update the ff lines depending on your setting.
  1. DB_DATABASE=post
  2. DB_USERNAME=root
  3. DB_PASSWORD=

Creating our Controller

Next we're gonna create a controller to handle our Post table that we are going to create 1. In command prompt, navigate to your project and type:
  1. php artisan make:controller PostController
This will create our controller in the form of PostController.php located in app/Http/Controllers folder. 2. Open PostController.php and edit it with the ff codes:
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use App\Post;
  7. use DB;
  8.  
  9. class PostController extends Controller
  10. {
  11. public function index(){
  12. return view('post');
  13. }
  14.  
  15. public function getDate(Request $request){
  16. $posts = DB::table('posts')
  17. ->whereBetween('created_at', [$request->fdate, $request->sdate])
  18. ->get();
  19.  
  20. return view('result', ['posts' => $posts]);
  21. }
  22. }

Creating our Model

1. In command prompt, navigate to our project and type:
  1. php artisan make:model Post -m
This will create our model Post.php located in app folder. It will also create the migration for us due to the -m that we added in creating the model in the form of create_posts_table.php file located in database/migrations folder. 2. Open this migration and edit it with ff codes:
  1. <?php
  2.  
  3. use Illuminate\Support\Facades\Schema;
  4. use Illuminate\Database\Schema\Blueprint;
  5. use Illuminate\Database\Migrations\Migration;
  6.  
  7. class CreatePostsTable extends Migration
  8. {
  9. /**
  10.   * Run the migrations.
  11.   *
  12.   * @return void
  13.   */
  14. public function up()
  15. {
  16. Schema::create('posts', function (Blueprint $table) {
  17. $table->increments('id');
  18. $table->text('post');
  19. $table->timestamps();
  20. });
  21. }
  22.  
  23. /**
  24.   * Reverse the migrations.
  25.   *
  26.   * @return void
  27.   */
  28. public function down()
  29. {
  30. Schema::dropIfExists('posts');
  31. }
  32. }

Making our Migration/Migrate

In command prompt, navigate to your project and type:
  1. php artisan migrate
If you have an error like this: [Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was t oo long; max key length is 767 bytes (SQL: alter table `users` add unique ` users_email_unique`(`email`)) You can solve this by opening AppServiceProvider.php located in app/Providers folder. Add this line:
  1. use Illuminate\Support\Facades\Schema;
In boot add this line:
  1. Schema::defaultStringLength(191);
migration error Run php artisan migrate again and make sure that your database is empty because it will have another error if its not.

Creating our Routes

In routes folder, open web.php and edit it with the ff codes:
  1. <?php
  2. Route::get('/', 'PostController@index');
  3.  
  4. Route::post('/select', 'PostController@getDate');

Creating our Views

In resources/views folder, create the ff files: app.blade.php
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Laravel Query Between 2 Dates</title>
  5. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
  6. <link rel="stylesheet" href="/css/app.css">
  7. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  8. </head>
  9. <body>
  10. @yield('content')
  11. </body>
  12. </html>
post.blade.php
  1. @extends ('app')
  2.  
  3. @section('content')
  4. <div class="container">
  5. <h1 class="page-header text-center">Laravel Query Between 2 Dates</h1>
  6. <div class="row">
  7. <div class="col-md-4 col-md-offset-4">
  8. <h2 class="text-center">Select 2 Dates</h2>
  9. <form method="POST" action="/select">
  10. {{ csrf_field() }}
  11. <div class="form-group">
  12. <label>First Date:</label>
  13. <input type="date" class="form-control" name="fdate">
  14. </div>
  15. <div class="form-group">
  16. <label>Second Date:</label>
  17. <input type="date" class="form-control" name="sdate">
  18. </div>
  19. <input type="submit" value="Get Post Between" class="btn btn-primary">
  20. </form>
  21. </div>
  22. </div>
  23. </div>
  24. @endsection
result.blade.php
  1. @extends ('app')
  2.  
  3. @section('content')
  4. <div class="container">
  5. <h1 class="page-header text-center">Laravel Query Between 2 Dates</h1>
  6. <div class="row">
  7.  
  8. <div class="col-md-8 col-md-offset-2">
  9. <h2>Results
  10. <a href="/" class="btn btn-primary pull-right">Back</a>
  11. </h2>
  12. @if(count($posts)>0)
  13. <table class="table table-bordered table-striped">
  14. <thead>
  15. <th>Post Date</th>
  16. <th>Post</th>
  17. </thead>
  18. <tbody>
  19. @foreach($posts as $post)
  20. <tr>
  21. <td>{{ date('M d, Y h:i A', strtotime($post->created_at)) }}</td>
  22. <td>{{ $post->post }}</td>
  23. </tr>
  24. @endforeach
  25. </tbody>
  26. </table>
  27. @else
  28. <h3 class="text-center">No Post from Selected Range</h3>
  29. @endif
  30. </div>
  31. </div>
  32. </div>
  33. @endsection

Running our Server

In your web browser, type the name that you added in localhost for your project in my case, wherebetween.dev. That ends this tutorial. Happy Coding :)

Add new comment