How to create QR code in Laravel Voyager

Liang Yu
Liang Yu
May 16 · 3 min read

It is very easy to create QR code in Laravel Voyager. Once you installed the Laravel framework and Voyager, you can edit the base controller of Voyager to enable the program to create a QR code for you.

Here is an example.

In my project, I need to create the QR code for different channels in order to marketing. So, in the settings of the BREAD of the table would be looking like this.

The BREAD settings for the table

As you can see, the ‘Controller Name’ has been override as “\App\Http\Controllers\Custom\ChannelController”. It is the path for the custom controller file located.

There, it requires to create a php file in this path which is called “ChannelController.php”.

Actually, you don’t have to build the controller from scratch. You can edit from the Voyager base controller

To create the QR code, just add few lines in the function “store” as below:

public function store(Request $request)
{
$slug = $this->getSlug($request);
$dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();
// Check permission
$this->authorize('add', app($dataType->model_name));
// Validate fields with ajax
$val = $this->validateBread($request->all(), $dataType->addRows);
if ($val->fails()) {
return response()->json(['errors' => $val->messages()]);
}
if (!$request->has('_validate')) {
$data = $this->insertUpdateData($request, $slug, $dataType->addRows, new $dataType->model_name());

// Create QRcode for the task and save it to our path
$channelId = $data->id;
$channelUrl = Config::get('constants.OATMINI.BASEURL').Config::get('constants.OATMINI.CHANNELINFO').$channelId;
$path="/channel-QRcode/".date("Ym",strtotime("today"));
if($this->new_folder($path))
{
$filename = $channelId.".png";

\QrCode::format('png')->size(100)->margin(0)->errorCorrection('H')->generate($channelUrl,'../storage/app/public'.$path."/".$filename);

}
$data->qrcode = $path."/".$filename;
$data ->save();

event(new BreadDataAdded($dataType, $data));
if ($request->ajax()) {
return response()->json(['success' => true, 'data' => $data]);
}
return redirect()
->route("voyager.{$dataType->slug}.index")
->with([
'message' => __('voyager::generic.successfully_added_new')." {$dataType->display_name_singular}",
'alert-type' => 'success',
]);
}
}

Meanwhile, in order to create a new folder for storing the QR code, a “new_folder” function is required at the end of this file.

private function new_folder($new_folder)
{

$success = false;
$error = '';

if (Storage::disk($this->filesystem)->exists($new_folder)) {
$success = true;
} elseif (Storage::disk($this->filesystem)->makeDirectory($new_folder)) {
$success = true;
} else {
$success = false;
}

return $success;
}

The url can be configured in the definition under the constants.php with the variable of channelId.

After deployed the file, when you create a new record in the table, a QR code can be created and stored in the “storage\app\public”.

The created QR code