PHP code example of beyondcode / laravel-vouchers

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/ */

    

beyondcode / laravel-vouchers example snippets


$videoCourse = VideoCourse::find(1);
$voucher = $videoCourse->createVoucher();

auth()->user()->redeemVoucher($voucher);



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');

$videoCourse = VideoCourse::find(1);

$videoCourse->createVouchers(2, [], today()->addDays(7));

$voucher = $user->redeemCode('ABCD-EFGH');

$user->redeemVoucher($voucher);

$voucher = $user->redeemCode('ABCD-EFGH');

$videoCourse = $voucher->model;

Vouchers::isValidCode('ABCD-EFGH'); // true or false
Vouchers::isValidVoucher($voucher); // true or false
bash
php artisan vendor:publish --provider="BeyondCode\Vouchers\VouchersServiceProvider" --tag="migrations"
bash
php artisan migrate
bash
php artisan vendor:publish --provider="BeyondCode\Vouchers\VouchersServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="BeyondCode\Vouchers\VouchersServiceProvider" --tag="translations"