faraweilyas's avatar

faraweilyas liked a comment+100 XP

5d ago

Use booted().

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Cookie\Middleware\EncryptCookies;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })
    ->booted(function (Application $app) {
        EncryptCookies::except(config('...'));
    })->create();
faraweilyas's avatar

faraweilyas wrote a comment+100 XP

2mos ago

This can be improved to bring down queries count:

// phase 3
foreach ($orderItems as $item) {
    OrderItem::create([
        'order_id' => $order->id,
        'product_id' => $item['product']->id,
        'quantity' => $item['quantity'],
        'price' => $item['price'],
        'subtotal' => $item['subtotal'],
    ]);

    $item['product']->decrement('stock_quantity', $item['quantity']);

    // ...
}

Single query to create order line items:

$orderLineItems = collect($orderItems)->map(fn ($item) => [
    'order_id' => $order->id,
    'product_id' => $item['product']->id,
    'quantity' => $item['quantity'],
    'price' => $item['price'],
    'subtotal' => $item['subtotal'],
]);

OrderItem::fillAndInsert($orderLineItems->toArray());

// can still be improved further by building an array of data we want to update first, then run a single mass update query
collect($orderItems)->each(fn ($item) => $item['product']->decrement('stock_quantity', $item['quantity']));

//...
faraweilyas's avatar

faraweilyas liked a comment+100 XP

2mos ago

Nice reminder about handling race conditions.

Just a small suggestion regarding this statement:

$product = Product::where('id', $item->product_id)
    ->lockForUpdate()
	->first();

That can be a bit simpler with whereKey:

$product = Product::whereKey($item->product_id)
    ->lockForUpdate()
	->first();
faraweilyas's avatar

faraweilyas wrote a comment+100 XP

3mos ago

This super powerful!

I have a suggestion though, this could be syntactic sugar....

we could have namespaced islands like so, using dot notation

@island(name: 'metrics.views-card')
    <div class="metrics-card">
        .....
    </div>
@endisland

@island(name: 'metrics.visitors-card')
    <div class="metrics-card">
        .....
    </div>
@endisland

Outside the islands

<!-- Target all islands -->
<button wire:click="refresh" wire:island="metrics.*">
  Refresh All Metrics
</button>

<!-- Target dedicated islands -->
<button wire:click="refresh" wire:island="metrics.views-card">
  Refresh views-card
</button>

<button wire:click="refresh" wire:island="metrics.visitors-card">
  Refresh visitors-card
</button>
faraweilyas's avatar

faraweilyas liked a comment+100 XP

3mos ago

Dude! This is awesome!

faraweilyas's avatar

faraweilyas wrote a comment+100 XP

4mos ago

Hello,

as referenced in the laravel docs, i think you might have the values swapped around at 10:00

'balanceMaxShift' => 3,
'balanceCooldown' => 1,

laravel docs: https://laravel.com/docs/12.x/horizon#balancing-strategies

'balanceMaxShift' => 1,
'balanceCooldown' => 3,
faraweilyas's avatar

faraweilyas liked a comment+100 XP

4mos ago

In Laravel 12, the behavior you're observing with Pint is likely due to changes in the default code style configuration. Pint is a code formatting tool that adheres to a set of predefined rules, and these rules can change between versions to align with evolving best practices or community preferences.

To address your issue, you have a couple of options:

  1. Customize Pint Configuration: You can customize the Pint configuration to match your preferred style. Pint uses a .pint.json file to define its rules. You can create or modify this file in the root of your project to specify your desired formatting rules.

    Here's an example of how you might configure Pint to adjust the formatting:

    {
        "preset": "laravel",
        "rules": {
            "method_argument_space": {
                "on_multiline": "ensure_fully_multiline"
            },
            "single_quote": true,
            "lowercase_keywords": true
        }
    }
    

    Adjust the rules as needed to achieve the formatting style you prefer.

  2. Check for Updates or Changes: Review the release notes or documentation for Laravel 12 and Pint to see if there are any mentions of changes to the default formatting rules. This can provide insight into why the formatting behavior has changed.

  3. Revert to Previous Version: If the new formatting style is not acceptable and you prefer the previous behavior, you could consider reverting to the version of Pint used in Laravel 11. However, this is generally not recommended as it may lead to compatibility issues with other tools or libraries.

By customizing the Pint configuration, you can regain control over the formatting style to better suit your preferences.

faraweilyas's avatar

faraweilyas liked a comment+100 XP

4mos ago

I'm creating a simple app with laravel 12, and I was writing the first tests, and I relized the pint does a weird formating.

it('Requires authentication ', function () {
get(route('dashboard'))->assertRedirect(route('login'));
    });

In my previous app with laravel 11 app, pint formats the same code like this. Wich I personally find way better.

it('requires authentication', function () {
    get(route('posts.create'))->assertRedirect(route('login'));
});

Is this a bug, or just a change on the default style?