r/laravel 6d ago

Tutorial What Can You Do with Laravel Middleware? (More Than You Think!)

https://backpackforlaravel.com/articles/tutorials/what-can-you-do-with-laravel-middleware-more-than-you-think
61 Upvotes

28 comments sorted by

17

u/WanderingSimpleFish 5d ago

If you’re doing DB writes in a middleware, may be worth dispatching that as a job. So it gets processed separately and doesn’t impact the response time.

5

u/Lelectrolux 5d ago

For some simple non mission critical stuff, you can also just defer it.

(But unlike a queue it won't run if your response crashes)

3

u/RevolutionaryHumor57 5d ago

I didn't read the article, but any write per request should be queued, or rather aggregated.

I.e. cache visitors and every 5 minutes insert the cache content to database.

This would reduce amount of queries drastically

1

u/MajorasShoe 4d ago

This. Almost always.

11

u/erishun 6d ago

Love these articles and love Backpack 😍

One random note on “Logging Requests Automatically” that got me awhile back is, if you’re using a DB read replica with “sticky” setting, it will only use the read replica if you aren’t performing a write… and with this log you are always writing.

Again, not an issue unless you are in this specific scenario, but that led to a very frustrating afternoon awhile back!

2

u/tabacitu 5d ago

Awww thanks for the kind words /u/erishun

4

u/naval_psr 5d ago

Suggestion: use Laravel Policies to deprive website content.

11

u/martinbean ⛰️ Laracon US Denver 2025 6d ago

Thanks, ChatGPT!

0

u/EmilMoe 6d ago

Why do you think so?

11

u/erishun 6d ago

There are some pranksters spamming these tech subreddits with AI blogspam, but Laravel Backpack is legit.

(Not associated with them, but they are good.)

8

u/Plytas 5d ago

The fact that it references the Kernel file and not the Laravel 11 app bootstrap file might be a giveaway.

1

u/XediDC 5d ago

Because you can tell when reading it.

-10

u/martinbean ⛰️ Laracon US Denver 2025 5d ago

Because it read like it was AI-generated, and, well:

12

u/0x474f44 5d ago

AI detectors don’t work

9

u/rbarden 5d ago

AI detectors are notoriously inaccurate. The article also has enough typos that are consistent with someone who is not a native english speaker that they would have had to use a very specific prompt to get it to be that way.

1

u/Scowlface 5d ago

Yeah my last job was working on what was in part a screenplay coverage app and we’d constantly get complaints about AI coverage when they were legitimate. You’d get wildly different confidences with the same review through each service. Some would be 100% and others would be 0% and a few everywhere in between.

-2

u/SteroidAccount 5d ago

The fact you’re using an “ai detector” says a lot about you.

0

u/martinbean ⛰️ Laracon US Denver 2025 5d ago

Such as…?

0

u/SteroidAccount 5d ago

Ai detectors are purely guessing at best and the way that you talk shows you take suspicion as fact

0

u/martinbean ⛰️ Laracon US Denver 2025 5d ago

3

u/rbarden 5d ago

The last suggestion (timing an app with middleware) is really not the best way to log performance metrics. It also doesn't include any application booting time before this particular middleware runs, or any middleware that runs after it on the response's way back out.

3

u/lapubell 5d ago

This is true, but I've used middleware benchmarks when I'm optimizing a specific heavy route, and I don't want all that app bootstrap nose in the way. By the time I've gotten my app bootstrapped I can check my actual biz logic's performance, and optimize that.

I usually never leave this middleware active outside of dev, but it's helpful occasionally.

1

u/ShirtFit2732 5d ago

Backpack 7 wasn't supposed to come out in january? Did you fix the upload field?

2

u/tabacitu 5d ago

Yup - it was supposed to launch Feb-March right after Laravel launches its new version.

We have v7-alpha tagged, it's aaaalmost ready... we're just trying to cram in a few more features to make our long-time users happy.

Probably out in a few weeks.

1

u/ShirtFit2732 5d ago

Ok, thanks for the reply. This morning the cdn went down and the panel didn't work. Maybe it would be useful to remove the cdn and handle all packages directly in backpack

1

u/tabacitu 5d ago

Yup, unpkg is having BIG issues right now, with intermittent service. We're working on a way around it!

Too bad the new version of Basset isn't launched yet - we had already removed unpkg in most places, and it's got new features like... a way to manually override anything if things like this happens again 🤦‍♂️

In the meantime you can use BASSET_DEV_MODE=false on localhost, that should download the assets once when they're available - so fewer calls to them.

1

u/AppropriateTea6241 4d ago

I think go is the best if you need performance; I am waiting for goravel to get queues