# How to Create QR Codes In Laravel 11

Interested in adding QR codes to your Laravel 11 project? This guide will show you how! Let's walk through the steps together to easily integrate QR code generation into your Laravel 11 application.

## Setting up Laravel 11

If you don't already have a Laravel application set up, you can simply create a new one by following the [installation guide in the docs](https://laravel.com/docs/11.x#creating-a-laravel-project) or simply running the following command:

```bash
composer create-project laravel/laravel example-app
```

Once installed, navigate into the `example-app` directory and start the Laravel dev server:

```bash
php artisan serve
```

## The Laravel QR Code Package

There is a cool package by [GiauPhan](https://github.com/giauphan) called [Laravel QR Code](https://packagist.org/packages/giauphan/laravel-qr-code) that provides an easy way to generate QR codes in a Laravel application.

To get started, install the package using composer:

```bash
composer require giauphan/laravel-qr-code
```

This package provides a `QRCode` [facade](https://laravel.com/docs/11.x/facades#how-facades-work) that we can use to create QR codes. So, let's get to work!

## Prepare a Route for Testing

If you don't already have a spot where you need to create a QR code and want to play around with the package, you can create a testing route.

Let's define a route in the `routes/web.php` file and call it `qr-code`:

```php
Route::get("/qr-code", function() {
    //
});
```

Inside the route handler function you can now put the code we're about to write to create QR codes.

## How to Use the QR Code Package

The facade provided by the package provides multiple methods to create different types of QR codes:

* Email message
    
* Phone number
    
* SMS
    
* Text
    
* URL
    
* MeCard (contact)
    
* vCard (contact)
    
* WiFi network settings
    

For example, to create a QR code that contains some text, there is the `text` method:

```php
use LaravelQRCode\Facades\QRCode;

QRCode::text("Hello World");
```

To directly output the QR code, use the `png` or `svg` methods:

```php
use LaravelQRCode\Facades\QRCode;

// PNG
QRCode::text("Hello World")->png();

// SVG
QRCode::text("Hello World")->svg();
```

We'll dive into other QR code types like URL and MeCard in just a second, but first let's take a look what else we can do with the package.

## Storing QR Codes as Images

A generated QR code can easily be saved on the filesystem using the `setOutfile` method. It accepts a path to save the file to which we can get using the [Laravel storage facade](https://laravel.com/docs/11.x/filesystem):

```php
use Illuminate\Support\Facades\Storage;
use LaravelQRCode\Facades\QRCode;

QRCode::text("Hello World")
    ->setOutfile(Storage::disk("public")->path("qrcode.png"))
    ->png();
```

With default Laravel configuration, this will save the file to `<project-directory>/storage/app/public/qrcode.png`. This works with the `svg` method too.

## Customizing the Image

### Changing the Size

By default, the pixel size is set to `4`. You can change it to a value from `1` to `10` to make it bigger or smaller using the `setSize` method:

```php
use LaravelQRCode\Facades\QRCode;

QRCode::text("Hello World")
    ->setSize(10)
    ->png();
```

### Changing the Margin

The margin is set `3` by default, but you can change it to any size from `1` to `10` or remove it completely by setting it to `0`:

```php
use LaravelQRCode\Facades\QRCode;

QRCode::text("Hello World")
    ->setMargin(2)
    ->png();
```

## Setting the Error Correction Level

| Level | Value | Bytes that can be restored |
| --- | --- | --- |
| Low | `L` | 7% |
| Medium | `M` | 15% |
| Quartile | `Q` | 25% |
| High | `H` | 30% |

The [error correction level](https://en.wikipedia.org/wiki/QR_code#Error_correction) can be set via the `setErrorCorrectionLevel` method:

```php
use LaravelQRCode\Facades\QRCode;

QRCode::text("Hello World")
    ->setErrorCorrectionLevel("Q")
    ->png();
```

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">When chaining multiple methods together, <strong>make sure the </strong><code>png</code><strong> or </strong><code>svg</code><strong> methods are called last</strong>. Both methods will save or output the QR code, so apply all required settings before.</div>
</div>

## More QR Code Types

### URL

```php
use LaravelQRCode\Facades\QRCode;

QRCode::url("https://linu.us")
    ->png();
```

### SMS

```php
use LaravelQRCode\Facades\QRCode;

QRCode::sms('+55 (31) 1234-5678', 'Text to send!')
    ->png();
```

### Phone Number

```php
use LaravelQRCode\Facades\QRCode;

QRCode::phone('+55 31 1234-5678')
    ->png();
```

### Email Message

```php
use LaravelQRCode\Facades\QRCode;

$to = 'john.doe@example.com';
$subject = 'QR Code Message';
$body = 'This email was created from a QR Code!';

QRCode::email($to, $body, $subject)
    ->png();
```

### MeCard

A [MeCard](https://en.wikipedia.org/wiki/MeCard_(QR_code)) stores information about a single contact.

```php
use LaravelQRCode\Facades\QRCode;

$name = 'John Doe';
$street = '1234 Main st.';
$phone = '+1 001 555-1234';
$email = 'john.doe@example.com';

QRCode::meCard($name, $street, $phone, $email)
    ->png();
```

### vCard

```php
use LaravelQRCode\Facades\QRCode;

// Personal Information
$firstName = 'John';
$lastName = 'Doe';
$title = 'Mr.';
$email = 'john.doe@example.com';
$company = "Acme Inc.";
$job = "Developer";
$url = "https://example.com";

// Addresses
$homeAddress = [
    'type' => 'home',
    'pref' => true,
    'street' => '123 my street st',
    'city' => 'My Beautiful Town',
    'state' => 'LV',
    'country' => 'Neverland',
    'zip' => '12345-678'
];
$wordAddress = [
    'type' => 'work',
    'pref' => false,
    'street' => '123 my work street st',
    'city' => 'My Dreadful Town',
    'state' => 'LV',
    'country' => 'Hell',
    'zip' => '12345-678'
];

$addresses = [$homeAddress, $wordAddress];

// Phones
$workPhone = [
    'type' => 'work',
    'number' => '001 555-1234',
    'cellPhone' => false
];
$homePhone = [
    'type' => 'home',
    'number' => '001 555-4321',
    'cellPhone' => false
];
$cellPhone = [
    'type' => 'work',
    'number' => '001 9999-8888',
    'cellPhone' => true
];

$phones = [$workPhone, $homePhone, $cellPhone];

QRCode::vCard($firstName, $lastName, $title, $email, $company, $job, $url, $addresses, $phones)
    ->setErrorCorrectionLevel('H')
    ->setSize(4)
    ->setMargin(2)
    ->svg();
```

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Since this QR code stores a lot of information, it's recommended to store it in a smaller pixel size and as SVG, so it can be re-scaled as needed. The error correction level should be set to high.</div>
</div>

## Conclusion

Thanks to the Laravel QR Code package it's really easy to create QR codes in Laravel. For more detailed information, check out the [GitHub repository](https://github.com/giauphan/laravel-qr-code) and the [official documentation](https://github.com/giauphan/laravel-qr-code/blob/master/docs/index.md).

---

**Thank you for reading!** 🙏

Do you find this article helpful? [Support my work on Hashnode](https://linu.us/sponsor) 💚

I'm also on [𝕏 / linusbenkner](https://x.com/linusbenkner) - would love to connect!

---

Happy coding 👨‍💻
