Sunday, October 18, 2020

Laravel custom helper

 

/**
* @return bool
*/
public function isLocalhost()
{
$list = array('127.0.0.1', '::1');
$host = $_SERVER['REMOTE_ADDR'];
if (in_array($host, $list) || strpos($host, '.test') !== false) {
// local
return true;
} else {
//production
return false;
}
}
/**************************
* Auth helpers
*************************/
/**
* @param null $guard
* @return bool
*/
public function isLoggedIn($guard = null)
{
return auth($guard)->check();
}
/**
* @param null $guard
* @return string|null
*/
public function getUsername($guard = null)
{
if (!auth($guard)->check()) return null;
return auth($guard)->user()->name;
}
/**
* @param null $guard
* @return int|null
*/
public function getUserId($guard = null)
{
if (!auth($guard)->check()) return null;
return auth($guard)->user()->id;
}
/**
* @param null $guard
* @return string|null
*/
public function getUserEmail($guard = null)
{
if (!auth($guard)->check()) return null;
return auth($guard)->user()->email;
}
/**
* @param null $guard
* @return object|null
*/
public function getCurrentUser($guard = null)
{
if (!auth($guard)->check()) return null;
return auth($guard)->user();
}
/***************************
* currency helper
***************************/
/**
* @param $amount
* @param int $decimal
* @return string
*/
public function toMoney($amount, $decimal = 2)
{
if (is_null($amount) || !is_numeric($amount)) return $amount;
return number_format($amount, $decimal);
}
/**
* @param $number
* @param array $option
* @return bool|mixed|string|null
*/ https://github.com/haruncpi/laravel-h/blob/master/src/H.php

php number to word code

public function numberToWord($number, $option = ['decimal' => 'dollar', 'fraction' => 'cents'])

    {

        $hyphen = '-';

        $conjunction = ' and ';

        $separator = ', ';

        $negative = 'negative ';

        $decimal = ' ' . $option['decimal'] . ' ';

        $afterFraction = $option['fraction'];

        $dictionary = array(0 => 'zero', 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six', 7 => 'seven', 8 => 'eight', 9 => 'nine', 10 => 'ten', 11 => 'eleven', 12 => 'twelve', 13 => 'thirteen', 14 => 'fourteen', 15 => 'fifteen', 16 => 'sixteen', 17 => 'seventeen', 18 => 'eighteen', 19 => 'nineteen', 20 => 'twenty', 30 => 'thirty', 40 => 'fourty', 50 => 'fifty', 60 => 'sixty', 70 => 'seventy', 80 => 'eighty', 90 => 'ninety', 100 => 'hundred', 1000 => 'thousand', 1000000 => 'million', 1000000000 => 'billion', 1000000000000 => 'trillion', 1000000000000000 => 'quadrillion', 1000000000000000000 => 'quintillion');


        if (!is_numeric($number)) {

            return false;

        }


        if (($number >= 0 && (int)$number < 0) || (int)$number < 0 - PHP_INT_MAX) {

            // overflow

            trigger_error('convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX, E_USER_WARNING);

            return false;

        }


        if ($number < 0) {

            return $negative . $this->numberToWord(abs($number));

        }


        $string = $fraction = null;


        if (strpos($number, '.') !== false) {

            list($number, $fraction) = explode('.', $number);

        }


        switch (true) {

            case $number < 21:

                $string = $dictionary[$number];

                break;

            case $number < 100:

                $tens = ((int)($number / 10)) * 10;

                $units = $number % 10;

                $string = $dictionary[$tens];

                if ($units) {

                    $string .= $hyphen . $dictionary[$units];

                }

                break;

            case $number < 1000:

                $hundreds = $number / 100;

                $remainder = $number % 100;

                $string = $dictionary[$hundreds] . ' ' . $dictionary[100];

                if ($remainder) {

                    $string .= $conjunction . $this->numberToWord($remainder);

                }

                break;

            default:

                $baseUnit = pow(1000, floor(log($number, 1000)));

                $numBaseUnits = (int)($number / $baseUnit);

                $remainder = $number % $baseUnit;

                $string = $this->numberToWord($numBaseUnits) . ' ' . $dictionary[$baseUnit];

                if ($remainder) {

                    $string .= $remainder < 100 ? $conjunction : $separator;

                    $string .= $this->numberToWord($remainder);

                }

                break;

        }


        if (null !== $fraction && is_numeric($fraction)) {

            $string .= $decimal;

            $words = array();

            foreach (str_split((string)$fraction) as $number) {

                $words[] = $dictionary[$number];

            }

            $string .= implode(' ', $words) . " " . $afterFraction;

        }


        return $string;

    }  

Laravel nested relation options

 case: user has many posts and post has many comments. we need user's posts with comments:

//Eloquent query:

Users::with('posts')->get();

models and relationships:

//In User model:

protected $with="comments"; //it is important

public function posts() {
    $this->hasMany('App\Post');
}

//In Post Model:

public function comments() {
    $this->hasMany('App\Comments');
}

all sites links list for laravel related docs.

 Laravel Docs sites: 

* https://www.positronx.io/

* https://www.codecheef.org/all-laravel-posts

* https://www.itsolutionstuff.com/category/laravelexample.html

* https://www.taniarascia.com/

* https://techsolutionstuff.com/

* https://blog.codehunger.in/

* https://hackthestuff.com/categories

1. https://scotch.io/tag/laravel

2. https://www.javatpoint.com/history-of-laravel

3. https://appdividend.com/category/laravel/

4. https://learn-laravel-queues.com/

5. https://divinglaravel.com/

6. https://laraveldaily.com/free-e-book-40-laravel-quick-tips-and-counting/

7. https://laravel-news.com/

8. https://devdojo.com/t/laravel

9. https://laravel-tricks.com/tricks

10. https://laracasts.com/

11. https://madewithlaravel.com/

12. https://dev.to/t/laravel

13.https://laraveldaily.com/

14.https://www.allphptricks.com/


Laravel Community  sites:

1. https://hashnode.com/post/taylor-otwell-cilmj90zh000k6t53il0rdgqp

2. https://laravel.io/


Tutorial sites:

1. https://scotch.io/

2. https://www.javatpoint.com

3. https://www.phptpoint.com/

4. https://appdividend.com/

5. https://devdojo.com

6. https://www.geeksforgeeks.org/


Css docs : 

1. https://stackcoder.in/posts/sass-or-scss-in-depth-tutorial

2. https://www.taniarascia.com/



Tuesday, October 13, 2020

How to create a Laravel Package

 https://devdojo.com/devdojo/how-to-create-a-laravel-package


https://medium.com/cafe24-ph-blog/build-your-own-laravel-package-in-10-minutes-using-composer-867e8ef875dd

Laravel service provider boot() vs register()

 As you can see in our new Service Provider class we have 2 methods boot() and register(). The boot() method is used to boot any routes, event listeners, or any other functionality you want to add to your package. The register() method is used to bind any classes or functionality into the app container