1. Go to this page and download the library: Download beyondcode/laravel-vouchers library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
return [
/*
* Database table name that will be used in migration
*/
'table' => 'vouchers',
/*
* Database pivot table name for vouchers and users relation
*/
'relation_table' => 'user_voucher',
/*
* List of characters that will be used for voucher code generation.
*/
'characters' => '23456789ABCDEFGHJKLMNPQRSTUVWXYZ',
/*
* Voucher code prefix.
*
* Example: foo
* Generated Code: foo-AGXF-1NH8
*/
'prefix' => null,
/*
* Voucher code suffix.
*
* Example: foo
* Generated Code: AGXF-1NH8-foo
*/
'suffix' => null,
/*
* Code mask.
* All asterisks will be removed by random characters.
*/
'mask' => '****-****',
/*
* Separator to be used between prefix, code and suffix.
*/
'separator' => '-',
/*
* The user model that belongs to vouchers.
*/
'user_model' => \App\User::class,
];
namespace App\Models;
use BeyondCode\Vouchers\Traits\CanRedeemVouchers;
class User extends Authenticatable
{
use CanRedeemVouchers;
# ...
}
namespace App\Models;
use BeyondCode\Vouchers\Traits\HasVouchers;
class VideoCourse extends Model
{
use HasVouchers;
# ...
}
$videoCourse = VideoCourse::find(1);
// Create 5 vouchers associated to the videoCourse model.
$vouchers = Vouchers::create($videoCourse, 5);
$videoCourse = VideoCourse::find(1);
// Returns an array of Vouchers
$vouchers = $videoCourse->createVouchers(2);
// Returns a single Voucher model instance
$vouchers = $videoCourse->createVoucher();
$videoCourse = VideoCourse::find(1);
$vouchers = $videoCourse->createVouchers(2, [
'from' => 'Marcel',
'message' => 'This one is for you. I hope you like it'
]);
$voucher = $user->redeem('ABC-DEF');
$from = $voucher->data->get('from');
$message = $voucher->data->get('message');