CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other
Showing posts with label laravel-packages. Show all posts
Showing posts with label laravel-packages. Show all posts

29 November, 2022

A Package for Ratings to be added to any Laravel Model

 Programing Coderfunda     November 29, 2022     Laravel, laravel-packages, php     No comments   

 


Cjmellor/rating is a Laravel package that allows for ratings to be added to a Model Imagine you have a Recipes Model and want your users to rate your culinary delights. This package allows you to achieve this.

Installation

You can install the package via composer:

composer require cjmellor/rating

You can publish and run the migrations with the following:

php artisan vendor:publish --tag="rating-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="rating-config"

Usage

To rate a Model, you must add the CanBeRated trait to the Model in question.

use Cjmellor\Rating\Concerns\CanBeRated;

class Recipe extends Model
{
    use CanBeRated;

    // ...
}

Now you can rate this Model:

$recipe = Recipe::find(1);

$recipe->rate(score: 2);

You can view how many ratings a particular Model has:

$recipe->ratings;

// this will return a Collection

You can get an overall percentage of the amount of Users' who have rated a Model:

Imagine you want a five-star rating system, and you have a Model that has been rated a 3 and a 5 by two Users'

$recipe->ratingPercent(maxLength: 5);

// 80.0

This will equate to 80%. A float is returned. Changing the maxLength will recalculate the percentage.

The package comes with a bunch of Attributes that you can use. The results of these are based on a single Model rated by two Users' with 3 and 5 ratings.

$recipe->averageRating; // "4.0000"
$recipe->averageRatingByUser; // "5.0000"
$recipe->averageSumOfUser; // 5
$recipe->ratedByUsers; // 2
$recipe->ratedInTotal; // 2
$recipe->sumRating; // "8" 

Blade Component

The package comes with an optional blade component for displaying the Models' ratings.

You must publish the file to get access to it

php artisan vendor:publish --tag="rating-component"

You can now use the new component:

<x-show-rating score="80.0" />

The component has customizable attributes:

public string $color = 'text-yellow-400',
public string $family = 'FontAwesome',
public $innerStars = '\f005 \f005 \f005 \f005 \f005',
public $outerStars = '\f006 \f006 \f006 \f006 \f006',
public float $score = 0.0,
public string $textSize = 'text-2xl',

For more details about this package, Please visit Github.

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

A Durable Workflow Engine in PHP Powered by Laravel Queues

 Programing Coderfunda     November 29, 2022     Laravel, laravel-packages, php     No comments   

 

Laravel Workflow is a Durable workflow engine that allows users to write long-running persistent distributed workflows (orchestrations) in PHP powered by Laravel Queues.

Installation

This library is installable via Composer. You must also publish the migrations for the workflows table.

composer require laravel-workflow/laravel-workflow

php artisan vendor:publish --provider="Workflow\Providers\WorkflowServiceProvider" --tag="migrations"

Usage

  1. Create a workflow.
class MyWorkflow extends Workflow
{
    public function execute()
    {
        $result = yield ActivityStub::make(MyActivity::class);
        return $result;
    }
}
  1. Create an activity.
class MyActivity extends Activity
{
    public function execute()
    {
        return 'activity';
    }
}
  1. Run the workflow.
$workflow = WorkflowStub::make(MyWorkflow::class);
$workflow->start();
while ($workflow->running());
$workflow->output();
=> 'activity'

Signals

Using WorkflowStub::await() along with signal methods allows a workflow to wait for an external event.

class MyWorkflow extends Workflow
{
    private bool $isReady = false;

    #[SignalMethod]
    public function ready()
    {
        $this->isReady = true;
    }

    public function execute()
    {
        $result = yield ActivityStub::make(MyActivity::class);

        yield WorkflowStub::await(fn () => $this->isReady);

        $otherResult = yield ActivityStub::make(MyOtherActivity::class);

        return $result . $otherResult;
    }
}

The workflow will reach the call to WorkflowStub::await() and then hibernate until some external code signals the workflow like this.

$workflow->ready();

Timers

Using WorkflowStub::timer($seconds) allows a workflow to wait for a fixed amount of time in seconds.

class MyWorkflow extends Workflow
{
    public function execute()
    {
        $result = yield ActivityStub::make(MyActivity::class);

        yield WorkflowStub::timer(300);

        $otherResult = yield ActivityStub::make(MyOtherActivity::class);

        return $result . $otherResult;
    }
}

The workflow will reach the call to WorkflowStub::timer() and then hibernate for 5 minutes. After that time has passed, it will continue execution.

Signal + Timer

In most cases you don't want to wait forever for a signal.

Instead, you want to wait for some amount of time and then give up. It's possible to combine a signal with a timer yourself to achieve this but a convenience method exists to do this for you, WorkflowStub::awaitWithTimeout().

$result = yield WorkflowStub::awaitWithTimeout(300, fn () => $this->isReady);

This will wait like the previous signal example but it will timeout after 5 minutes. If a timeout occurs, the result will be false.

Failed Workflows

If a workflow fails or crashes at any point then it can be resumed from that point. Any activities that were successfully completed during the previous execution of the workflow will not be run again.

$workflow = WorkflowStub::load(1);
$workflow->resume();
while ($workflow->running());
$workflow->output();
=> 'activity'

Retries

A workflow will only fail when the retries on the failing activity have been exhausted.

The default activity retry policy is to retry activities forever with an exponential backoff that decays to 2 minutes. If your activity fails because of a transient error (something that fixes itself) then it will keep retrying and eventually recover automatically. However, if your activity fails because of a permanent error then you will have to fix it manually via a code deploy and restart your queue workers. The activity will then retry again using the new code and complete successfully.

Workflows and activities are based on Laravel Queues so you can use any options you normally would.

For more details and source code, Please visit Github

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

A Coupons and Promotional Codes Generator for Laravel

 Programing Coderfunda     November 29, 2022     Laravel, laravel-packages, php     No comments   

 


Laravel-promocodes is an awesome coupon and promotional code generator for Laravel. The current release is for Laravel 9.x and PHP 8.1. It's completely rewritten, if you use the previous version, you should change your code accordingly. The code is simplified now and it should take you several minutes to completely rewrite usage.

Installation

You can install the package via composer:

composer require zgabievi/laravel-promocodes

Configuration

php artisan vendor:publish --provider="Zorb\Promocodes\PromocodesServiceProvider"

After you configure this file, run migrations:

php artisan migrate

Now you will need to use AppliesPromocode on your user model.

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Zorb\Promocodes\Traits\AppliesPromocode;

class User extends Authenticatable {
    use AppliesPromocode;

    //
}

Usage

It's very easy to use. Methods are combined so that you can configure promo codes easily.

Creating Promo codes

Using class

Combine methods as you need. You can skip any method that you don't need, most of them already have default values.

use Zorb\Promocodes\Facades\Promocodes;

Promocodes::mask('AA-***-BB') // default: config('promocodes.code_mask')
          ->characters('ABCDE12345') // default: config('promocodes.allowed_symbols')
          ->multiUse() // default: false
          ->unlimited() // default: false
          ->boundToUser() // default: false
          ->user(User::find(1)) // default: null
          ->count(5) // default: 1
          ->usages(5) // default: 1
          ->expiration(now()->addYear()) // default: null
          ->details([ 'discount' => 50 ]) // default: []
          ->create();

Using helper

There is a global helper function that will do the same as promocodes class. You can use named arguments magic from php 8.1.

createPromocodes(
    mask: 'AA-***-BB', // default: config('promocodes.code_mask')
    characters: 'ABCDE12345', // default: config('promocodes.allowed_symbols')
    multiUse: true, // default: false
    unlimited: true, // default: false
    boundToUser: true, // default: false
    user: User::find(1), // default: null
    count: 5, // default: 1
    usages: 5, // default: 1
    expiration: now()->addYear(), // default: null
    details: [ 'discount' => 50 ] // default: []
);

Using command

There is also the command for creating promocodes. Parameters are optional here too.

php artisan promocodes:create\
  --mask="AA-***-BB"\
  --characters="ABCDE12345"\
  --multi-use\
  --unlimited\
  --bound-to-user\
  --user=1\
  --count=5\
  --usages=5\
  --expiration="2022-01-01 00:00:00"

Generating Promocodes

If you want to output promocodes and not save them to the database, you can call generate method instead of create.

use Zorb\Promocodes\Facades\Promocodes;

Promocodes::mask('AA-***-BB') // default: config('promocodes.code_mask')
          ->characters('ABCDE12345') // default: config('promocodes.allowed_symbols')
          ->multiUse() // default: false
          ->unlimited() // default: false
          ->boundToUser() // default: false
          ->user(User::find(1)) // default: null
          ->count(5) // default: 1
          ->usages(5) // default: 1
          ->expiration(now()->addYear()) // default: null
          ->details([ 'discount' => 50 ]) // default: []
          ->generate();

Applying Promocode

Using class

Combine methods as you need. You can skip any method that you don't need.

use Zorb\Promocodes\Facades\Promocodes;

Promocodes::code('ABC-DEF')
          ->user(User::find(1)) // default: null
          ->apply();

Using helper

There is a global helper function that will do the same as the promocodes class.

applyPomocode(
    'ABC-DEF',
    User::find(1) // default: null
);

Using command

There is also the command for applying promocode.

php artisan promocodes:apply ABC-DEF --user=1

For more details and source code, please visit Github.

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Automatically Restart Horizon When Local PHP Files Change

 Programing Coderfunda     November 29, 2022     Laravel, laravel-packages, php     No comments   

 



How many hours have lost debugging local jobs only to find out that you forgot to restart Horizon?

Laravel Horizon Watcher package contains an Artisan command horizon:watch that will start Horizon and automatically restart it when any PHP file is created, updated, or deleted.

This command is meant to be used in the local environment.

Installation

You can install the package via composer:

composer require spatie/laravel-horizon-watcher --dev

In your project, you should have the JavaScript package chokidar installed. You can install it.

via npm

npm install chokidar

or Yarn

yarn add chokidar

Optionally, you can publish the config file with this command:

php artisan vendor:publish --tag="horizon-watcher-config"

Usage

Run this command

php artisan horizon:watch

to start Horizon. When a PHP file in your project gets created, updated, or deleted, Horizon will automatically restart.

For more details and source code, please visit Github.

Closing Note

The team of Codebrisk Laravel developers is always ready to execute even your boldest ideas. Our expert team can design and develop any type of custom CRM solution, SAAS app, or e-commerce app to meet our customer's needs and transform our customer's experiences. Get in touch with our team to discuss your bespoke ideas and learn more about the next steps to launching cooperation.

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

A Set of Amazing Laravel Validation Rules for Credit Card

 Programing Coderfunda     November 29, 2022     Laravel, laravel-packages, php     No comments   

 

This is a Set of Amazing Laravel Validation Rules for Credit Cards that will validate that a given credit card number, expiration date, or CVC is valid.

Usage

As FormRequest

<?php

namespace App\Http\Requests;

use LVR\CreditCard\CardCvc;
use LVR\CreditCard\CardNumber;
use LVR\CreditCard\CardExpirationYear;
use LVR\CreditCard\CardExpirationMonth;
use Illuminate\Foundation\Http\FormRequest;

class CreditCardRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'card_number' => ['required', new CardNumber],
            'expiration_year' => ['required', new CardExpirationYear($this->get('expiration_month'))],
            'expiration_month' => ['required', new CardExpirationMonth($this->get('expiration_year'))],
            'cvc' => ['required', new CardCvc($this->get('card_number'))]
        ];
    }
}

Card number

From request

$request->validate(
    ['card_number' => '37873449367100'],
    ['card_number' => new LVR\CreditCard\CardNumber]
);

Directly

(new LVR\CreditCard\Cards\Visa)
    ->setCardNumber('4012888888881881')
    ->isValidCardNumber()

Card Expiration

From request

// CardExpirationYear requires card expiration month
$request->validate(
    ['expiration_year' => '2017'],
    ['expiration_year' => ['required', new LVR\CreditCard\CardExpirationYear($request->get('expiration_month'))]]
);

// CardExpirationMonth requires card expiration year
$request->validate(
    ['expiration_month' => '11'],
    ['expiration_month' => ['required', new LVR\CreditCard\CardExpirationMonth($request->get('expiration_year'))]]
);

// CardExpirationDate requires date format
$request->validate(
    ['expiration_date' => '02-18'],
    ['expiration_date' => ['required', new LVR\CreditCard\CardExpirationDate('my')]]
);

Directly

LVR\CreditCard\Cards\ExpirationDateValidator(
    $expiration_year,
    $expiration_month
)->isValid();

// Or static
LVR\CreditCard\Cards\ExpirationDateValidator::validate(
    $expiration_year,
    $expiration_month
);

Card CVC

From request

// CardCvc requires card number to determine allowed cvc length
$request->validate(
    ['cvc' => '123'],
    ['cvc' => new LVR\CreditCard\CardCvc($request->get('card_number'))]
);

Directly

LVR\CreditCard\Cards\Card::isValidCvcLength($cvc);

For more details, Please visit Github.

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Older Posts Home

Meta

Popular Posts

  • Sitaare Zameen Par Full Movie Review
     Here’s a  complete Vue.js tutorial for beginners to master level , structured in a progressive and simple way. It covers all essential topi...
  • Credit card validation in laravel
      Validation rules for credit card using laravel-validation-rules/credit-card package in laravel Install package laravel-validation-rules/cr...
  • AI foot tracking model
    I am a student doing a graduation project. I urgently need to deal with this model (I am attaching a link). I've never worked with pytho...
  • Write API Integrations in Laravel and PHP Projects with Saloon
    Write API Integrations in Laravel and PHP Projects with Saloon Saloon  is a Laravel/PHP package that allows you to write your API integratio...
  • iOS 17 Force Screen Rotation not working on iPAD only
    I have followed all the links on Google and StackOverFlow, unfortunately, I could not find any reliable solution Specifically for iPad devic...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (69)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • July (4)
  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

Loading...

Laravel News

Loading...

Copyright © CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda