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!
Step # 1 : Create fresh Laravel project or use existing project.
Two commands to create fresh laravel project.
Global Command : laravel new invoice
Or use
Non Global Command : composer create-project laravel/laravel --prefer-dist invoice
Step # 2 : Access the project.
Open a terminal (e.g., Git Bash) and navigate to your Laravel project's root folder.
Git Bash : cd c:xampp/htdocs/invoice
Step # 3 : Install Invoice package.
Run the following command to install the package.
Command : composer require laraveldaily/laravel-invoices
Step # 4 : Publish the configuration file & views.
If you want to customize the package's configuration, you can publish the config file.
Command : php artisan vendor:publish --tag=invoices.config
To customize the invoice templates, publish the views.
Command : php artisan vendor:publish --tag=invoices.views
Step # 5 : Create a controller.
Run the following command to create the InvoiceController.
Command : php artisan make:controller InvoiceController
Then, update the InvoiceController 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();
}
}
Step # 6 : Create a route.
Import InvoiceController class and create a route.
use App\Http\Controllers\InvoiceController;
Route::get('/invoice', [InvoiceController::class, 'generateInvoice']);
Step # 7 : It's time to test.
Clear all cached configurations, views, routes, and more.
Command: php artisan optimize:clear
Start the Laravel development server.
Command : php artisan serve
Access below 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 then see the generated invoice.
You can enhance the invoice by customizing item descriptions, applying discounts, adding a unique invoice number, and including seller details. Also, consider custom styling and specifying payment terms.
Let’s enhance the invoice by adding more items, updating the seller's details, changing the invoice name, and ensuring it is downloaded correctly. Here's the updated code
<?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();
}
}
Access below URL
127.0.0.1:8000/invoice
The file will be downloaded by the name Codeshotcut-invoice.
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
For more details, please refer to the 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