PHP code example of tanthammar / laravel-extras

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

    

tanthammar / laravel-extras example snippets


// Find users whose email starts with 'john'
User::whereStartsWith('email', 'john')->get();

// Chain with other conditions
User::where('active', true)->orWhereStartsWith('name', 'admin')->get();

// Find users with gmail addresses
User::whereEndsWith('email', 'gmail.com')->get();

// Find users where BOTH name and email contain 'john'
User::whereAllLike(['name', 'email'], 'john')->get();

// Find users where name OR email contains 'john'
User::where('active', true)->orWhereAnyLike(['name', 'email'], 'john')->get();

// Find users where name contains both 'john' AND 'doe' (in any order)
User::whereContains('name', 'john doe')->get();

// OR condition with contains
User::where('active', true)->orWhereContains(['name', 'email'], 'john doe')->get();

// Search posts by content, title, or author name
Post::whereRelationsLike(['title', 'content', 'author.name'], 'laravel')->get();

// Find events where localized name starts with 'Summer'
Event::whereTranslatableStartsWith('name', 'Summer')->get();

// Search specific locale
Event::whereTranslatableStartsWith('name', 'Été', 'fr')->get();

// Find events where localized name contains 'festival'
Event::whereTranslatableLike('name', 'festival')->get();

// Find events where localized name contains 'music festival' (both words)
Event::whereTranslatableContains('name', 'music festival')->get();

// Find bookings that overlap with a specific date range
Booking::whereOverlaps('start_date', 'end_date', '2024-01-01', '2024-01-31')->get();

// Check if user exists by ID
if (User::existsById(123)) {
    // User exists
}

// Check if record exists by UUID
if (Product::existsByUuid('550e8400-e29b-41d4-a716-446655440000')) {
    // Product exists
}

// Sort by translated name field
Event::orderByTranslation('name', 'asc')->get();

// Sort by regular field with locale collation
User::orderByLocale('name', 'desc')->get();

use TantHammar\LaravelExtras\CleanNumber;

$phone = CleanNumber::clean('+46 (0)70-123 45 67');
// Result: '46701234567'

use TantHammar\LaravelExtras\NoWhiteSpace;

$clean = NoWhiteSpace::clean("Hello\n\tWorld   ");
// Result: 'HelloWorld'

use TantHammar\LaravelExtras\MarkdownToHtmlString;

$html = new MarkdownToHtmlString('**Bold text** with [link](https://example.com)');
// Use in Blade templates
{!! $html !!}

// Filament placeholder field example
Placeholder::make(trans('fields.accounting-chart'))
    ->disableLabel()
    ->content(new MarkdownToHtmlString(__('fields.account_hint')))
    ->columnSpan('full')

use TantHammar\LaravelExtras\ArrHelper;

// Remove empty values from array
$clean = ArrHelper::filledOnly(['name' => 'John', 'email' => '', 'age' => null]);
// Result: ['name' => 'John']

// Swap array items (using Arr::swap macro)
$assocArray = [
    'item_one'   => ['name' => 'One'],
    'item_two'   => ['name' => 'Two'],
    'item_three' => ['name' => 'Three'],
    'item_four'  => ['name' => 'Four'],
];

$newArray = Arr::swap($assocArray, 'item_one', 'item_three');
/*
 * Result:
 * [
 *     'item_three' => ['name' => 'Three'],
 *     'item_two'   => ['name' => 'Two'],
 *     'item_one'   => ['name' => 'One'],
 *     'item_four'  => ['name' => 'Four'],
 * ]
 */

use TantHammar\LaravelExtras\MoneyIntegerCast;

class Product extends Model
{
    protected $casts = [
        'price' => MoneyIntegerCast::class,
    ];
}

// Store $19.99 as 1999 (integer)
$product = new Product();
$product->price = 19.99;  // Stored as 1999
echo $product->price;     // Outputs: 19.99

use TantHammar\LaravelExtras\OCR3;

// Generate unique OCR number
$ocr = OCR3::unique();
// Result: '1234567890' (with valid check digit)

// Generate with specific length
$ocr = OCR3::unique(8);
// Result: '12345678' (8 digits with check digit)

// Use in factories
$factory->define(Invoice::class, function (Faker $faker) {
    return [
        'ocr_number' => $faker->ocr3(),
    ];
});

use TantHammar\LaravelExtras\SKU;

$sku = SKU::generate('Premium Widget');
// Result: 'PW-ABC123' (source prefix + random signature)

use TantHammar\LaravelExtras\PrettyPrint;

// In PHP
PrettyPrint::dump($complexArray);

// In Blade templates
@prettyPrint($data)
bash
php artisan vendor:publish --provider="TantHammar\LaravelExtras\LaravelExtrasServiceProvider" --tag="migrations"
php artisan migrate