Skip to content

Latest commit

 

History

History
227 lines (159 loc) · 6.93 KB

others.md

File metadata and controls

227 lines (159 loc) · 6.93 KB

Other Useful Tips (cd ..)

Laravel Tip 💡: isset() (⬆️)

Did you know you could pass multiple arguments to the isset() function?

<?php

if(isset($var1) && isset($var2)) { ... }
if(isset($var1, $var2)) { ... }

Laravel Tip 💡: Preview Mailables (⬆️)

When working with mailables, we often send them to MailHog or Mailtrap to quickly preview the rendered email. Did you know that Laravel allows you to preview emails in the browser as if they were regular Blade files? 🚀

<?php

Route::get('/mailable', function () {
    $invoice = App\Models\Invoice::find(1);

    return new App\Mail\InvoicePaid($invoice);
});

PHP Tip 💡: More Readable Numbers (⬆️)

If you work with numbers, the longer they are, the harder they are to read. Did you know you can use underscores for better readability? 🚀

<?php

$amount = 1000;
$amount = 1_000;

$amount = 100000;
$amount = 100_000;

$amount = 100000000;
$amount = 100_000_000;

Laravel Tip 💡: Bootable Traits (⬆️)

Did you know that Laravel automatically boots your traits if they follow the boot[TraitName] convention? This allows you to easily define shared logic for model events. And here's a secret: that's where multi-tenancy starts 🚀

<?php

trait Sluggable
{
    public static function bootSluggable()
    {
        static::saving(function ($model) {
            $model->slug = str($model->title)->slug()->toString();
        });
    }
}

class Post extends Model
{
    use Sluggable; // The trait will be booted automatically
}

Laravel Tip 💡: Requests Fingerprints (⬆️)

Have you ever needed to code a unique identifier for a request, such as for caching purposes? Laravel ships with the "fingerprint" method that allows you to generate a unique identifier for your requests 🚀

<?php

// Generates a unique fingerprint using the domain, URI, method and IP address
request()->fingerprint() // fbb969117edfa916b86dfb67fd11decf1e336df0

Laravel Tip 💡: Work with IPs (⬆️)

Sometimes you may need to work with IP addresses. Laravel uses the HttpFoundation component from Symfony under the hood, which comes with useful IP helpers 🚀

use Symfony\Component\HttpFoundation\IpUtils;

$ipv4 = '192.168.1.1';

IpUtils::checkIp4($ipv4); // true, "checkIp6" is also available
IpUtils::isPrivateIp($ipv4); // true, works with IPv6 as well
IpUtils::anonymize($ipv4); // '192.168.1.0', works with IPv6 as well

Laravel Tip 💡: The Conditionable Trait (⬆️)

Do you find yourself writing multiple if statements to call different methods on a class? Consider making your class "conditionable" by using Laravel's "Conditionable" trait 🚀

<?php

use Illuminate\Support\Traits\Conditionable;

class NotificationService
{
    use Conditionable;

    public function sendEmailNotification()
    {
        echo "Email notification sent.";
    }

    public function sendSmsNotification()
    {
        echo "SMS notification sent.";
    }
}

$notificationService = new NotificationService;

$shouldSendEmail = true;
$shouldSendSms = false;

// Instead of this
if ($shouldSendEmail) {
    $notificationService->sendEmailNotification();
}

if ($shouldSendSms) {
    $notificationService->sendSmsNotification();
}

// You can do this
$notificationService
    ->when($shouldSendEmail, fn(NotificationService $service) => $service->sendEmailNotification())
    ->when($shouldSendSms, fn(NotificationService $service) => $service->sendSmsNotification());

Laravel Tip 💡: On-Demand Notifications (⬆️)

Sometimes, you may want to send notifications to "anonymous" users who are not yet stored in the database. Laravel's "route" method allows you to do exactly that 🚀

<?php

use Illuminate\Broadcasting\Channel;
use Illuminate\Support\Facades\Notification;

Notification::route('mail', '[email protected]')
    ->route('twilio', '9419287384')
    ->route('slack', '#slack-channel')
    ->notify(new InvoicePaid($invoice));

Laravel Tip 💡: Send All Mail to a Specific Email (⬆️)

Ever needed to send all mails to a single address? Whether you're working in a specific environment or managing a small project with just one contact email, you can use the "alwaysTo" method to do exactly that 🚀

<?php

use Illuminate\Support\Facades\Mail;

public function boot(): void
{
    if (app()->environment('staging')) {
        Mail::alwaysTo('[email protected]');
    }
}

Laravel Tip 💡: The New Flexible Cache Method (⬆️)

Starting with Laravel v11.23.0, you can use the new "flexible" method for caching. If you've struggled with revalidating your cache before it expires, make use of it 🚀

<?php

// If a request is made between the 5 and 10s interval,
// the cache will be recalculated 🔥 The cache will only expire after 10 seconds
// if no requests are made within the 5 to 10s window.
$value = Cache::flexible('users', [5, 10], fn () => User::all());

Laravel Tip 💡: Retrieve and Delete Items From the Session (⬆️)

We often need to retrieve an item from the session and then delete it. While you can use the usual combo of get and forget, Laravel ships with the "pull" method that does exactly that 🚀

<?php

// Instead of this 😫
$value = session()->get('key', 'default-value');

session()->forget('key');

// Do this 😎
$value = session()->pull('key', 'default-value');

Laravel Tip 💡: On-Demand Storage Disks (⬆️)

Have you ever needed to quickly create a disk, whether for tests or temporary files, but had to define it in the filesystem configuration? Well, Laravel ships with on-demand disks so that you can define disks at runtime instead 🚀

<?php

use Illuminate\Support\Facades\Storage;
 
$disk = Storage::build([
    'driver' => 'local',
    'root' => '/path/to/root',
]);
 
$disk->put('image.jpg', $content);