How to install laravel 9
https://tutorial101.blogspot.com/2022/02/how-to-install-laravel-9.html
Connecting our Database
open .env file root directory.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
php artisan make:model Post -m
C:\xampp\htdocs\laravel\blog>php artisan make:model Post -m
Open new events migrations
yourproject/database/migrations
blog\database\migrations\_create_post_table.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create( 'posts' , function (Blueprint $table ) { $table ->increments( 'id' ); $table ->text( 'post' ); $table ->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists( 'posts' ); } }; |
php artisan migrate
C:\xampp\htdocs\laravel9\blog>php artisan migrate
Migration table created successfully.
check database table
Creating Controller
php artisan make:controller PostController
C:\xampp\htdocs\laravel\blog>php artisan make:controller PostController
change it with the following codes:
blog\app\Http\Controllers\PostController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | //blog\app\Http\Controllers\PostController.php <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use DB; class PostController extends Controller { public function index(){ return view( 'post' ); } public function getDate (Request $request ){ $posts = DB::table( 'posts' ) ->whereBetween( 'created_at' , [ $request ->fdate, $request ->sdate]) ->get(); return view( 'result' , [ 'posts' => $posts ]); } } |
blog\resources\views\app.blade.php
1 2 3 4 5 6 7 8 9 10 11 | //blog\resources\views\app.blade.php <!DOCTYPE html> <html> <head> <title>Laravel Query Between 2 Dates</title> <link rel= "stylesheet" href= "https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" /> </head> <body> @yield( 'content' ) </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | //blog\resources\views\result.blade.php @ extends ( 'app' ) @section( 'content' ) <div class = "container" > <h1 class = "page-header text-center" >Laravel Query Between 2 Dates</h1> <div class = "row" > <div class = "col-md-8 col-md-offset-2" > <h2>Results <a href= "/" class = "btn btn-primary pull-right" >Back</a> </h2> @ if ( count ( $posts )>0) <table class = "table table-bordered table-striped" > <thead> <th>Post Date </th> <th>Post</th> </thead> <tbody> @ foreach ( $posts as $post ) <tr> <td>{{ date ( 'M d, Y h:i A' , strtotime ( $post ->created_at)) }}</td> <td>{{ $post ->post }}</td> </tr> @ endforeach </tbody> </table> @ else <h3 class = "text-center" >No Post from Selected Range</h3> @ endif </div> </div> </div> @endsection |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | //blog\resources\views\post.blade.php @ extends ( 'app' ) @section( 'content' ) <div class = "container" > <h1 class = "page-header text-center" >Laravel Query Between 2 Dates</h1> <div class = "row" > <div class = "col-md-4 col-md-offset-4" > <h2 class = "text-center" >Select 2 Dates</h2> <form method= "POST" action= "/select" > {{ csrf_field() }} <div class = "mb-3" > <label>First Date :</label> <input type= "date" class = "form-control" name= "fdate" > </div> <div class = "mb-3" > <label>Second Date :</label> <input type= "date" class = "form-control" name= "sdate" > </div> <input type= "submit" value= "Submit" class = "btn btn-primary" > </form> </div> </div> </div> @endsection |
blog\routes\web.php
1 2 3 4 5 6 7 8 9 10 11 12 | //blog\routes\web.php <?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\PostController; //add PostController //Route::get('/', function () { // return view('welcome'); //}); Route::get( '/' , [PostController:: class , 'index' ]); Route::post( '/select' , [PostController:: class , 'getDate' ]); |
Starting Laravel development server: http://127.0.0.1:8000