Tuesday, December 22, 2020

vue js laravel validation Vee Validate

 

Vee Validate for Laravel backend validation

Simple vue js plugin that makes it easier to show validation errors from Laravel validation by using vee-validate.

Getting Started

In your script entry point:

import Vue from 'vue';
import VeeValidate from 'vee-validate';
import VeeValidateLaravel from 'vee-validate-laravel';
 
Vue.use(VeeValidate);
Vue.use(VeeValidateLaravel);
 

From Laravel:

 
$request->validate([
    'name' => 'required|min:3|max:255'
]);
 
 

In Vue classes:

 
<template>
    <div class="form-group" v-bind:class="{'has-error' : errors.has('name')}">
        <label for="name">Name</label>
        <input 
            type="text" 
            name="name"
            class="form-control"
            v-model="name"
            v-validate="'required'" />
        <div v-show="errors.has('name')" class="help-block">{{ errors.first('name') }}</div>
    </div>
</template>
 
<script>
    export default {
        methods: {
            doValidation() {
                const data = {
                    name: this.name
                };
            
                axios.post('/example', data).then(res => {
                }).catch(err => {
                    this.$setLaravelValidationErrorsFromResponse(err.response.data);
                });
            }
        }
    }
</script>
 


Tuesday, December 15, 2020

Mysql database Backup+Restore

 #Backup MySql Database

mysqldump -u root -p sitedb > /var/www/sitedomain/db/sitedomaindb_20.sql;

mysqldump -u root -p site_db > sitedomain_db.sql;

#Restore MySql Database
sudo mysql -u root -p site_db < /var/www/sitedo_main/dev/db/sitedo_maindb.sql

Monday, December 7, 2020

Laravel singleton vs bind

 Use bind for reusable classes or objects - the object is constructed each time it is called. If you need multiple instances of a class, use bind

Use singleton for a class or object that you need access to throughout the application - the object is only constructed once and so retains state throughout execution. If you only need a single, shared, instance of a class, use singleton

For example:

app\Support\TestClass.php

<?php

namespace App\Support;

class TestClass
{
    protected $value = 0;

    public function increase()
    {
        $this->value++;

    return $this->value;
    }
}

AppServiceProvider.php

...
public function register()
{
    $this->app->singleton(
        'test1',
        \App\Support\TestClass::class
    );

    $this->app->bind(
        'test2',
        \App\Support\TestClass::class
    );
}
...

Tinker:

>>> app('test1')->increase()
=> 1
>>> app('test1')->increase()
=> 2
>>> app('test1')->increase()
=> 3

>>> app('test2')->increase()
=> 1
>>> app('test2')->increase()
=> 1
>>> app('test2')->increase()
=> 1

Sunday, December 6, 2020

Send email to those users whos TrialExpiringSoon schedule command

<?php

namespace App\Console\Commands;

use App\Mail\TrialExpiringSoon;
use App\Team;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;

class EmailTeamsWithExpiringTrials extends Command
{
    protected $signature = 'ohdear:email-teams-with-expiring-trials';

    protected $description = 'Email teams with expiring trials.';

    protected $mailsSent = 0;

    protected $mailFailures = 0;

    public function handle()
    {
        $this->info('Sending trial expiring soon mails...');

        Team::all()
            ->filter->onSoonExpiringTrial()
            ->each(function (Team $team) {
                $this->sendTrialEndingSoonMail($team);
            });

        $this->info("{$this->mailsSent} trial expiring mails sent!");
                
        if ($this->mailFailures > 0) {
           $this->error("Failed to send {$this->mailFailures} trial expiring mails!");
        }
    }

    protected function sendTrialEndingSoonMail(Team $team)
    {
        try {
            if ($team->wasAlreadySentTrialExpiringSoonMail()) {
                return;
            }
                        
            $this->comment("Mailing {$team->owner->email} (team {$team->name})");
            Mail::to($team->owner->email)->send(new TrialExpiringSoon($team));

            $this->mailsSent++;

            $team->rememberHasBeenSentTrialExpiringSoonMail();
        } catch (Exception $exception) {
            $this->error("exception when sending mail to team {$team->id}", $exception);
            report($exception);
            $this->mailFailures++;
        }
    }
}

 

Saturday, November 28, 2020

Laravel sslcommerze intregation code demo

 $sslcommerz = new SSLCommerz();

        $sslcommerz->setPaymentDisplayType('hosted'); // enum('hosted', 'checkout')
        $sslcommerz->setPrimaryInformation([
            'total_amount' => (float) Str::round($item->kl_charge_paid),
            'currency'     => 'USD', //$item->currency,
        ]);
        $sslcommerz->setTranId($this->generateTransaction()); // set your transaction id here
        $sslcommerz->setSuccessUrl(route('payment.success'));
        $sslcommerz->setFailUrl(route('payment.failed'));
        $sslcommerz->setCancelUrl(route('payment.cancel'));
        $sslcommerz->setCustomerInformation([
            'cus_name'     => $buyer->fullName,
            'cus_add1'     => $buyer->country_id,
            'cus_city'     => $buyer->country_id,
            'cus_postcode' => $buyer->country_id,
            'cus_country'  => $buyer->country_id,
            'cus_email'    => $buyer->email,
            'cus_phone'    => $buyer->mobile,
        ]);
        $sslcommerz->setShipmentInformation([
            // 'ship_name'       => 'Store Test',
            // 'ship_add1'       => 'Dhaka',
            // 'ship_add2'       => 'Dhaka',
            // 'ship_city'       => 'Dhaka',
            // 'ship_state'      => 'Dhaka',
            // 'ship_postcode'   => '1000',
            // 'ship_country'    => 'Bangladesh',
            'shipping_method' => 'NO',
        ]);

        $sslcommerz->setAdditionalInformation([
            'value_a' => $type,
            'value_b' => $item->item_description['reference_id'],
            'value_c' => $buyer->encrypt,
            'value_d' => $item->item_id,
        ]);

        $sslcommerz->setEmiOption(0); // enum(1, 0)
        $sslcommerz->setProductInformation([
            'product_name'     => $item->item_description['item_name'],
            'product_category' => 'Goods',
            'product_profile'  => 'physical-goods',
        ]);

        $sslcommerz->setProductAmount((float) Str::round($item->total_amount));

        $response = $sslcommerz->initPayment($sslcommerz);
                                                            //dd($response);
        return redirect()->to($response['GatewayPageURL']); // redirect to gateway page url