Are you working on a Laravel project and need to retrieve data from the past six months? Laravel provides powerful tools to make this task simple and efficient. In this article, we’ll walk through the process of fetching data from the last six months using Laravel’s Eloquent ORM (Object-Relational Mapping) in a straightforward and easy-to-understand manner.
Step 1: Set Up Your Laravel Project
First, make sure you have Laravel installed on your system. If not, you can follow the official Laravel documentation to get started.
Once your Laravel project is set up, navigate to your project directory in the terminal and create a new model and migration for the data you want to retrieve. For example, if you want to fetch data from a “sales” table, you can create a model and migration like so:
php artisan make:model Sale -m
This command will generate a model file (Sale.php
) in the app
directory and a migration file in the database/migrations
directory.
Step 2: Define the Database Schema
In your migration file (e.g., create_sales_table.php
), define the schema for your table. For example, if your “sales” table has a created_at
column to store the date and time of each sale, your migration file might look like this:
Schema::create('sales', function (Blueprint $table) {
$table->id();
$table->decimal('amount', 8, 2);
$table->dateTime('created_at');
$table->timestamps();
});
Don’t forget to run the migration to create the table in your database:
php artisan migrate
Step 3: Fetch Data from the Last Six Months
Now, let’s retrieve data from the “sales” table for the last six months using Laravel’s Eloquent ORM. Open your Sale.php
model file and add the following method:
use Illuminate\Database\Eloquent\Model;
class Sale extends Model
{
public function scopeLastSixMonths($query)
{
return $query->where('created_at', '>=', now()->subMonths(6));
}
}
This method defines a local scope named lastSixMonths
that filters records based on the created_at
column, selecting only those that are six months old or newer.
Step 4: Retrieve Data in Your Controller
Now that we’ve defined the scope in our model, we can easily retrieve data from the last six months in our controller. Open your controller file (e.g., SalesController.php
) and add the following code:
use App\Models\Sale;
class SalesController extends Controller
{
public function index()
{
$sales = Sale::lastSixMonths()->get();
return view('sales.index', compact('sales'));
}
}
This code fetches all sales records from the last six months using the lastSixMonths
scope we defined earlier and passes them to a view called index
.
Step 5: Display Data in Your View
Finally, you can display the fetched data in your view file (index.blade.php
). Here’s a simple example of how you can iterate over the sales records and display them:
@foreach($sales as $sale)
<p>{{ $sale->amount }} - {{ $sale->created_at }}</p>
@endforeach
Conclusion
In this article, we’ve learned how to fetch data from the last six months using Laravel’s Eloquent ORM. By following these steps, you can easily retrieve and display the data you need for your Laravel project. Happy coding!
- Check our tools small Tools
- Check our tools website Word count