How to Get Month Wise Data in Laravel 11 | websolutioncode.com
How to Get Month Wise Data in Laravel 11 | websolutioncode.com

How to Get Month Wise Data in Laravel 11

Introduction:

In Laravel development, fetching data based on months is a common requirement. Whether you’re building a dashboard, generating reports, or analyzing trends, being able to retrieve data month-wise is essential. In this guide, we’ll explore a straightforward approach to achieve this in Laravel 11, accompanied by practical code examples. By the end, you’ll have a clear understanding of how to retrieve month-wise data efficiently.

Step 1:

Setup Your Laravel Environment Before diving into the implementation, ensure you have Laravel 11 installed in your development environment. You can set up Laravel by following the official documentation on Laravel’s website.

Step 2:

Database Setup Assuming you have a database table containing records with a date column, ensure it’s properly configured. For demonstration purposes, let’s consider a hypothetical “transactions” table with a “created_at” column storing the transaction dates.

Step 3:

Writing the Query In Laravel, fetching month-wise data involves leveraging the eloquent ORM. Let’s construct a query to retrieve data for a specific month.

use App\Models\Transaction;
use Illuminate\Support\Facades\DB;

$month = 5; // Replace with the desired month
$year = 2024; // Replace with the desired year

$data = Transaction::select(DB::raw('SUM(amount) as total'))
->whereMonth('created_at', $month)
->whereYear('created_at', $year)
->first();

$totalAmount = $data->total ?? 0;

echo "Total amount for month {$month}, {$year}: {$totalAmount}";

Explanation:

  • We import the Transaction model at the top.
  • Using the DB facade, we construct a query to select the total sum of the “amount” column from the “transactions” table.
  • We apply whereMonth and whereYear constraints to filter records based on the provided month and year.
  • Finally, we retrieve the total amount and display it.

Step 4:

Implementing in Controller To integrate this functionality into your Laravel application, you can create a dedicated controller method. Below is a basic example of how you can structure it:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Transaction;
use Illuminate\Support\Facades\DB;

class TransactionController extends Controller
{
public function getTotalAmountByMonth(Request $request)
{
$month = $request->input('month');
$year = $request->input('year');

$data = Transaction::select(DB::raw('SUM(amount) as total'))
->whereMonth('created_at', $month)
->whereYear('created_at', $year)
->first();

$totalAmount = $data->total ?? 0;

return response()->json(['total_amount' => $totalAmount]);
}
}

Step 5:

Route Configuration Don’t forget to define the route for your controller method in the routes/web.php file:

use App\Http\Controllers\TransactionController;

Route::get('/total-amount-by-month', [TransactionController::class, 'getTotalAmountByMonth']);

Conclusion:

In this guide, we’ve explored a straightforward approach to retrieve month-wise data in Laravel 11. By following the outlined steps and code examples, you can efficiently fetch data based on specific months for your application needs. Remember to adapt the code snippets to fit your project’s requirements, and feel free to explore further customization options offered by Laravel’s powerful ORM capabilities.