Laravel 11 - How to Generate Invoices.
Laravel 11 - How to Generate Invoices.
In this tutorial, we’ll show how to generate and customize invoices in Laravel 11 to meet specific requirements, making billing flexible and tailored to your needs.
If you're a video person, feel free to skip the post and check out the video instead!
Quick Overview
This guide walks you through creating an invoice generation system in a Laravel application. It starts with creating a fresh Laravel project or using an existing one. Next, the laraveldaily/laravel-invoices package is installed, and its configuration and views are published. A controller, InvoiceController, is created to generate an invoice, which includes defining buyer and seller details, invoice items, and customizing the invoice's appearance and filename. Routes are then set up to handle the invoice generation request. Finally, the application is tested by clearing cached configurations, running the server, and accessing the invoice URL. If needed, additional customizations are made to the invoice layout and content.
Step # 1 : Create fresh Laravel project or use existing project.
If Laravel is installed globally, create a new project using.
laravel new invoice
Or
if Laravel is not installed globally, you can create a new project using Composer.
composer create-project laravel/laravel --prefer-dist invoice
During setup, follow these choices when prompted.
- Would you like to install the starter kit? — Select none
- After selecting None, you'll be asked about testing framework. — Select Pest
- After selecting Pest, you'll be asked to select the database for your application. — Select mysql
- After selecting MySql, you'll be asked if you want to run the default database migration. — Type yes
Step # 2 : Access the project.
Open a terminal (e.g., Git Bash) and navigate to your Laravel project's root folder.
cd c:xampp/htdocs/invoice
Step # 3 : Install the Invoice package.
Run the following command to install the Laravel Invoices package.
composer require laraveldaily/laravel-invoices
This command will download and install the laraveldaily/laravel-invoices package along with its dependencies, adding it to the vendor directory and updating the composer.json file. If the package includes service providers or configuration files, you may need to publish them manually. However, in most cases, the package should work out of the box without the need for further configuration.
Step # 4 : Publish the configuration file & views.
To customize the package's configuration, run.
php artisan vendor:publish --tag=invoices.config
This will publish the configuration file to config/invoices.php, allowing you to modify settings like currency, default invoice format, and other invoice-related options
To customize the invoice templates, run.
php artisan vendor:publish --tag=invoices.views
This will publish the invoice view files to resources/views/vendor/invoices/, enabling you to modify the layout and design of generated invoices.
Step # 5 : Create a controller.
Run the following command to generate the InvoiceController.
php artisan make:controller InvoiceController
Then, update the controller with the following code to generate an invoice using the package.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use LaravelDaily\Invoices\Invoice;
use LaravelDaily\Invoices\Classes\Buyer;
use LaravelDaily\Invoices\Classes\InvoiceItem;
class InvoiceController extends Controller
{
public function generateInvoice()
{
// Define the Buyer information
$customer = new Buyer([
'name' => 'Code Shotcut',
'custom_fields' => [
'email' => 'codeshotcut@gmail.com',
],
]);
// Define Invoice Items
$items = [
(new InvoiceItem())->title('Product 1')->pricePerUnit(50),
(new InvoiceItem())->title('Service 1')->pricePerUnit(100),
];
// Create the Invoice
$invoice = Invoice::make()
->buyer($customer)
->date(now())
->addItems($items)
->currencySymbol('$')
->currencyCode('USD');
// Display the Invoice
return $invoice->stream();
}
}
This controller defines a generateInvoice() method that creates an invoice with buyer details and multiple items. It uses the Buyer class to define both customer and seller information and the InvoiceItem class for the invoice items. After the invoice is created, it is generated and streamed as a PDF to the user. The invoice will be displayed directly in the browser, where users can download it. You can further customize the controller to add more information, apply custom formatting, or include additional features like discounts, invoice numbers, and more. With this setup, your Laravel application now supports dynamic invoice generation and can be easily extended with additional features.
Step # 6 : Create a Route.
Import the InvoiceController class and create a route to generate the invoice.
use App\Http\Controllers\InvoiceController;
Route::get('/invoice', [InvoiceController::class, 'generateInvoice']);
This route will trigger the generateInvoice() method in InvoiceController when accessing /invoice, generating and displaying the invoice as a PDF.
Step # 7 : It's time to test.
To test the invoice generation, first, clear all cached configurations, views, and routes by running the following command.
php artisan optimize:clear
Next, start the Laravel development server.
php artisan serve
Then, access the URL.
127.0.0.1:8000/invoice
If you encounter an Internal Server Error: Class NumberFormatter not found, open the php.ini file and uncomment the following line.
From
;extension=intl
To
extension=intl
Save the file, restart the server, and refresh the page. You should now be able to see the generated invoice.
To enhance the Invoice, you can customize item descriptions, apply discounts, add a unique invoice number, include seller details, apply custom styling, and specify payment terms. Here's an updated version of the code, which adds more items, updates the seller's details, and changes the invoice name.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use LaravelDaily\Invoices\Invoice;
use LaravelDaily\Invoices\Classes\Buyer;
use LaravelDaily\Invoices\Classes\InvoiceItem;
class InvoiceController extends Controller
{
public function generateInvoice()
{
// Define the Buyer information
$customer = new Buyer([
'name' => 'All About Laravel',
'custom_fields' => [
'email' => 'allaboutlaravel@gmail.com',
],
]);
// Define the Seller information using Buyer
$seller = new Buyer([
'name' => 'Code Shotcut',
'address' => '123 Main St, City, State, ZIP',
'email' => 'codeshotcut@gmail.com',
'phone' => '123-456-7890',
'custom_fields' => [
'website' => 'https://codeshotcut.com',
'SWIFT' => 'BANKABCXYZ',
],
]);
// Define Invoice Items
$items = [
(new InvoiceItem())->title('Product 1')->pricePerUnit(50),
(new InvoiceItem())->title('Service 1')->pricePerUnit(100),
(new InvoiceItem())->title('Product 2')->pricePerUnit(75),
(new InvoiceItem())->title('Service 2')->pricePerUnit(150),
(new InvoiceItem())->title('Product 3')->pricePerUnit(200),
(new InvoiceItem())->title('Service 3')->pricePerUnit(125),
(new InvoiceItem())->title('Product 4')->pricePerUnit(300),
];
// Create the Invoice
$invoice = Invoice::make()
->buyer($customer)
->seller($seller) // Use the Buyer class for seller details
->date(now())
->addItems($items)
->currencySymbol('$')
->currencyCode('USD');
// Set custom filename
$invoice->filename('Codeshotcut-invoice');
// Download the Invoice
return $invoice->download();
}
}
Once you access the URL 127.0.0.1:8000/invoice, the file will be downloaded as Codeshotcut-invoice.pdf. You can continue customizing the invoice further by updating item descriptions, applying custom styles, and including payment terms or discounts.
If you want to customize the layout or change how the content is displayed, you can modify the default view provided by the package. After publishing the views (if not done already), the template will be available at resources/views/vendor/invoices/default.blade.php. You can then edit this file to adjust the styling, layout, or structure of the invoice according to your needs.
Conclusion
By following this guide, you've successfully implemented an invoice generation system in your Laravel application. You can now generate, customize, and download invoices with detailed buyer and seller information, multiple items, and custom layouts. The laraveldaily/laravel-invoices package simplifies the process of creating professional invoices, and with a few adjustments, you can tailor the system to fit your specific needs. You can further enhance the invoice layout, apply discounts, add unique invoice numbers, and customize payment terms.
For more details, refer to the laravel-invoices package documentation.
Share this with friends!
To engage in commentary, kindly proceed by logging in or registering
Subscribe to Our Newsletter
Stay ahead of the curve! Join our newsletter to see what everyone’s talking about.
0 Comments