How to Search Date From and To Dynamic Database Using Laravel 6

How to Search Date From and To Dynamic Database Using Laravel 6

Laravel 6 - Search Data Between Two Dates (Dynamic Date Filter)

In Laravel 6, you can filter data dynamically between two dates using Eloquent ORM or Query Builder. Below is a step-by-step guide to implement it.

1. Setup Database & Model

Ensure your database is properly set up in .env file:

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_database DB_USERNAME=root DB_PASSWORD=

If you don’t have a table, create a migration for a sample orders table:

php artisan make:migration create_orders_table

Modify database/migrations/xxxx_xx_xx_create_orders_table.php:

use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateOrdersTable extends Migration { public function up() { Schema::create('orders', function (Blueprint $table) { $table->id(); $table->string('customer_name'); $table->decimal('amount', 10, 2); $table->date('order_date'); $table->timestamps(); }); } public function down() { Schema::dropIfExists('orders'); } }

Run migration:

php artisan migrate

Create an Eloquent model for Order:

php artisan make:model Order

Modify app/Order.php:

namespace App; use Illuminate\Database\Eloquent\Model; class Order extends Model { protected $fillable = ['customer_name', 'amount', 'order_date']; }

2. Create Controller for Date Filtering

Run the command to create a controller:

php artisan make:controller OrderController

Modify app/Http/Controllers/OrderController.php:

namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Order; class OrderController extends Controller { public function index(Request $request) { $query = Order::query(); // If 'from' and 'to' dates are provided, apply the filter if ($request->has('from') && $request->has('to')) { $from = $request->input('from'); $to = $request->input('to'); $query->whereBetween('order_date', [$from, $to]); } $orders = $query->get(); return view('orders.index', compact('orders')); } }

3. Create Blade View for Date Search Form

Create resources/views/orders/index.blade.php:

<!DOCTYPE html> <html> <head> <title>Search Orders by Date Range</title> </head> <body> <h2>Search Orders</h2> <form method="GET" action="{{ url('/orders') }}"> <label>From Date:</label> <input type="date" name="from" required> <label>To Date:</label> <input type="date" name="to" required> <button type="submit">Search</button> </form> @if(isset($orders)) <h3>Order Results</h3> <table border="1"> <tr> <th>ID</th> <th>Customer Name</th> <th>Amount</th> <th>Order Date</th> </tr> @foreach ($orders as $order) <tr> <td>{{ $order->id }}</td> <td>{{ $order->customer_name }}</td> <td>${{ $order->amount }}</td> <td>{{ $order->order_date }}</td> </tr> @endforeach </table> @endif </body> </html>

4. Define Route

Modify routes/web.php:

use App\Http\Controllers\OrderController; Route::get('/orders', [OrderController::class, 'index']);

5. Run the Application

Start the Laravel development server:

php artisan serve

Visit:

http://127.0.0.1:8000/orders
  • Select From Date and To Date, then click Search.

  • It will display the filtered orders based on the selected date range.

6. Alternative: Using Query Builder

If you prefer Query Builder instead of Eloquent: Modify OrderController.php:

use Illuminate\Support\Facades\DB; public function index(Request $request) { $query = DB::table('orders'); if ($request->has('from') && $request->has('to')) { $query->whereBetween('order_date', [$request->from, $request->to]); } $orders = $query->get(); return view('orders.index', compact('orders')); }

Conclusion

Dynamic date search using whereBetween().
Eloquent ORM or Query Builder for flexibility.
Simple HTML form for user-friendly filtering.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close