Laravel Simple Month Selection

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Laravel Simple Month Selection - A Comprehensive Guide Introduction When working on web applications using Laravel, you often need to filter or group data based on different criteria such as dates. One of the common tasks is selecting a month from a date field. This blog post aims to help you understand how to perform this task easily and effectively using Laravel's query builder. We will discuss the problem, provide a thorough approach, share code examples, and elaborate on best practices. The Problem In the provided scenario, the user wants to retrieve data from a table named "ordenes", where one of the fields is "fechas" containing datetime values. The user attempted to use SQL's MONTH function to filter the query based on the month of the date field, but it didn't work as expected. Solution: Using Laravel's Query Builder Laravel provides an elegant and efficient way to perform database queries using its query builder. The first step is to instantiate a new connection object. In this case, we would use the connection defined in the configuration file or any other relevant method. ```php use Illuminate\Support\Facades\DB; // Or use DB::connection('your_db_connection'); ``` Next, we could retrieve all the data from the "ordenes" table utilizing select() and grouping by the required columns: ```php $data = DB::table("ordenes") ->select(array('*', DB::raw('((cant_ped)*(precio_unit)) as sum_pedidos'), DB::raw('((cant_pend)*(precio_unit)) as sum_pends'), DB::raw('((cant_rec)*(precio_unit)) as sum_recibidos'), DB::raw('(((cant_pend)*(precio_unit)) + ((cant_rec)*(precio_unit))) as sum_importe'))) //...rest of the query ``` Filtering by Month Using Carbon Laravel's Query Builder doesn't support functions directly, but we can use the Carbon library to achieve our objective. Carbon is a PHP library for working with dates and times. Let's integrate it into our code: ```php use Carbon\Carbon; ``` Modify your query as follows: ```php // Date in ISO format (YYYY-MM-DD) $filtered_date = $some_variable; // Set it based on the input or condition you want to use $data = DB::table("ordenes") ->select(array('*', DB::raw('((cant_ped)*(precio_unit)) as sum_pedidos'), DB::raw('((cant_pend)*(precio_unit)) as sum_pends'), DB::raw('((cant_rec)*(precio_unit)) as sum_recibidos'), DB::raw('(((cant_pend)*(precio_unit)) + ((cant_rec)*(precio_unit))) as sum_importe'))) ->where('cod_prov', '<>', 0) // Filter based on the condition for 'cod_prov' ->where(function($query) use ($filtered_date) { // Custom where clause $query->whereRaw("DATE_FORMAT(fechas, '%Y%m')", Carbon::createFromFormat('Ymd', $filtered_date)->format('%Y%m')) }) ->groupBy('cod_prov') ->skip(Input::get("jtStartIndex")) ->take(Input::get("jtPageSize")) ->get(); ``` In this example, we use Carbon to format our date as required by the DATE_FORMAT function. Then, we create a custom where clause to check if the formatted date matches our condition defined with $filtered_date. Conclusion By using Laravel's query builder and integrating it with the powerful Carbon library, you can achieve complex data filtering tasks easily and efficiently. This blog post has provided a comprehensive guide on how to perform month selection in your Laravel applications. Remember that LaravelCompany offers various resources, including articles and tutorials, to help improve your web development skills and stay up-to-date with the latest technologies.