PHP code example of internetguru / laravel-common

1. Go to this page and download the library: Download internetguru/laravel-common 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/ */

    

internetguru / laravel-common example snippets


'providers' => ServiceProvider::defaultProviders()->merge([
    InternetGuru\LaravelCommon\ReadOnlyServiceProvider::class,
])->toArray(),

use Illuminate\Support\ServiceProvider;

'providers' => ServiceProvider::defaultProviders()->replace([
    Illuminate\Translation\TranslationServiceProvider::class => InternetGuru\LaravelCommon\TranslationServiceProvider::class,
])->toArray(),

Route::get('/example', ExampleController::class)
    ->withoutMiddleware(\InternetGuru\LaravelCommon\Http\Middleware\PreventDuplicateSubmissions::class);

// Controller - the e-mail is normalized in the validated array *and* in the request
$validated = $request->validate(['email' => '

// Livewire - the property itself is rewritten, which is what the component reads next
public string $email = '  Fó[email protected]  ';

$this->validate(['email' => '

// config/ig-common.php
'sanitize' => [
    'pipelines' => [
        'account_number' => ['base', 'strip:\s', 'upper'],
        'tel' => ['base', App\Sanitizers\PhoneNumber::class],
    ],

    'types' => [
        'account_number' => 'account_number',
        '*_account' => 'account_number',
        'mobile' => 'tel',
    ],
],

use InternetGuru\LaravelCommon\Traits\SanitizesInput;

class Feedback extends Component
{
    use SanitizesInput;

    public array $formData = [];

    protected function sanitizeTypes(): array
    {
        return [
            'formData.0' => 'email',
            'formData.1' => 'text',
        ];
    }

    public function send(): void
    {
        $data = $this->validate($rules);

        $data['formData'][0];   // "[email protected]"
        $this->formData[0];     // "  Fó[email protected]  " - as it was typed
    }
}

$types = $this->app['config']->get('ig-common.sanitize.types');

if (is_array($types)) {
    $this->app['config']->set('ig-common.sanitize.types', [
        'register' => 'flag',
        'remember' => 'flag',
        ...$types,   // entries the application already set win
    ]);
}

    use Illuminate\Support\Facades\Facade;

    'aliases' => Facade::defaultAliases()->merge([
        'Helpers' => InternetGuru\LaravelCommon\Support\Helpers::class,
    ])->toArray(),
    

use Carbon\Carbon;
use Illuminate\Support\Number;
use Illuminate\Support\Str;

echo Str::ref(6);
// Output: "k3mhpq"

Number::useCurrency('USD');
echo Number::currency(1234, precision: 0);
// Output (en locale): $1,234
echo Number::currencySymbol();
// Output (en locale): $
echo Number::currencySymbol('CZK', 'cs');
// Output: Kč

$date = Carbon::parse('2023-12-31');
echo $date->dateForHumans();
// Output (en_US locale): 12/31/2023
$dateTime = Carbon::parse('2023-12-31 18:30:00');
echo $dateTime->dateTimeForHumans();
// Output (en_US locale): 12/31/2023 6:30 PM

    'breadcrumb_skip_prefixes' => [
        '_debugbar',
        '_ignition',
        'livewire',
        'storage',
        'telescope',
        'horizon',
    ],
    

    
    Route::get('/', function () {
        // ...
    })->name('home');

    Route::get('/products', function () {
        // ...
    })->name('products.index');

    Route::get('/products/{product}', function ($product) {
        // ...
    })->name('products.show');
    

    
    return [
        'home' => 'Long Application Name|LAN',
        'products.index' => 'All Products|Products',
        'products.show' => 'Product Details',
    ];
    

$this->dispatch('ig-message', type: 'success', message: 'Item saved!');
$this->dispatch('ig-message', type: 'danger', message: 'Something went wrong.');

enum OrderStatus: string implements HasLabel
{
    use RendersLabel;

    case PAID = 'paid';
    case UNPAID = 'unpaid';

    public function label(): string
    {
        return match ($this) {
            self::PAID => __('order.status.paid'),
            self::UNPAID => __('order.status.unpaid'),
        };
    }

    public function variant(): ?string
    {
        return match ($this) {
            self::PAID => 'success',
            self::UNPAID => 'danger',
        };
    }
}

    use InternetGuru\LaravelCommon\Traits\AssociationHistory;

    class Reservation extends Model
    {
        use AssociationHistory;

        protected array $associationHistoryTracked = ['status', 'note'];
    }
    

    'association_history' => [
        'columns' => [
            \App\Models\Reservation::class => 'reservation.history.column',
        ],
    ],
    

use Illuminate\Database\Eloquent\Model;
use InternetGuru\LaravelCommon\Casts\CarbonIntervalCast;

class Task extends Model
{
    protected $casts = [
        'duration' => CarbonIntervalCast::class,
    ];
}

use InternetGuru\LaravelCommon\Traits\Ulid32;

class Order extends Model
{
    use Ulid32;
}

// Class-based
use InternetGuru\LaravelCommon\Rules\Ulid32;

$request->validate([
    'code' => ['e' => '

use InternetGuru\LaravelCommon\Services\GeolocationService;

$geoService = app(GeolocationService::class);
$location = $geoService->getLocation('8.8.8.8');

echo $location->timezone; // "America/Chicago"
echo $location->country;  // "US"

use InternetGuru\LaravelCommon\Notifications\BaseNotification;

class OrderConfirmation extends BaseNotification
{
    public function toMail(object $notifiable): MailMessage
    {
        return (new \InternetGuru\LaravelCommon\Mail\MailMessage)
            ->setExtraMailData($this->getExtraMailData())
            ->subject('Order Confirmed')
            ->view(['html' => 'emails.order-confirmed', 'text' => 'emails.order-confirmed-text']);
    }
}

'channels' => [
    'json_daily' => [
        'driver' => 'custom',
        'via' => InternetGuru\LaravelCommon\Logging\JsonDailyLogger::class,
        'path' => storage_path('logs/app.log'),
        'days' => 14,
        'level' => 'debug',
    ],
],
html
<x-ig::label text="Paid" variant="success" />

<x-ig::label :text="$location->name" :seed="$location->name" />

<x-ig::label text="Administrator" variant="danger" icon="fa-solid fa-user-gear" />

<x-ig::label text="Hidden" variant="secondary" slim />
bash
    php artisan vendor:publish --tag=ig-common:migrations
    php artisan migrate
    
bash
php artisan vendor:publish --tag=ig-common:migrations
php artisan migrate
bash
# Publish database migrations
php artisan vendor:publish --tag=ig-common:migrations

# Publish configuration
php artisan vendor:publish --tag=ig-common:config

# Publish views for customization
php artisan vendor:publish --tag=ig-common:views

# Publish language files
php artisan vendor:publish --tag=ig-common:lang