andriajah Fri Apr 2024 2 weeks ago

Contoh Generate QR Code di Laravel 11

Pada postingan kali ini saya akan menunjukkan cara membuat QR Code di aplikasi Laravel 11. Kami akan membuat dan menyimpan QR Code untuk link.

Kami akan menggunakan paket composer simplesoftwareio/simple-qrcode untuk menghasilkan kode QR di Laravel 11. simplesoftwareio/simple-qrcode menyediakan metode untuk menghasilkan QR Code, menyimpan QR Code, menghasilkan QR Code untuk tautan, menghasilkan kode QR untuk nomor telepon, menghasilkan QR Code untuk email, dan buat QR Code dengan unduhan. Jadi, kita akan melihat contoh kode satu per satu.

1: Contoh Laravel Generate QR Code untuk Link

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        return QrCode::size(300)->generate('https://www.medikre.com');
    }
}

 

2: Contoh Laravel Genrate QR Code dan Simpan

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $path = public_path('qrcode/'.time().'.png');
  
        return QrCode::size(300)
                     ->generate('A simple example of QR code', $path);
    }
}

 

3: Contoh Laravel Generate QR Code dengan Warna

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        return QrCode::size(300)
                     ->backgroundColor(255,55,0)
                     ->generate('A simple example of QR code');
    }
}

 

4: Contoh Laravel Generate QR Code dengan Gambar

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $image = QrCode::format('png')
                         ->merge(public_path('images/YOUR_IMAGE_NAME.png'), 0.5, true)
                         ->size(500)
                         ->errorCorrection('H')
                         ->generate('A simple example of QR code!');
  
        return response($image)->header('Content-type','image/png');
    }
}

 

5: Contoh Laravel Generate QR Code Email

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        return QrCode::size(500)
                 ->email('[email protected]', 'Welcome to Medikre.com!', 'This is !.');
    }
}

 

6: Contoh Laravel Generate QR Code Telepon

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        return QrCode::phoneNumber('111-222-6666');
    }
}

 

7: Contoh Laravel Generate QR Code SMS

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class QRCodeController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        QrCode::SMS('111-222-6666', 'Body of the message');
    }
}

 

8: Contoh Laravel Generate QR Code dalam File Blade

<div class="visible-print text-center">
    {!! QrCode::size(100)->generate('Demo'); !!}
    <p>Scan me to return to the original page.</p>
</div>

 

Saya harap ini dapat membantu Anda...

laravel 11