Master Laravel with hand-curated daily tips

Laravel documentation is top-notch, but it's impossible to cover everything. Mastering Laravel is not just solving problems when you're stuck - it's also about becoming the best Artisan and programmer you can be.

We're excited to share a daily email with you that you can read in less than 2 minutes. Oh, and if daily is too often - don't worry - you can opt into weekly digests instead.

What are you waiting for?!

Or check out the archive below to be convinced.
I've been subscribed to this newsletter for less than a week, and I already received some advice that I was able to instantly implement into my daily work. samuel Samuel Pedraza

Daily Tips Archive

We've been cranking out tips for a while now. Can you believe there are 183 of them?! Keep on scrolling to see what you may have missed below.


Is now the right time to clean up this data?

Keep a realistic outlook

Using the stricter assertSame check with collections

Without making the test more complex

Should this be a collection or an array?

Use the right tool for the job

A strategy for testing permissions logic

And avoiding false positives

Easier test coverage for complex API resources

And the tests are fast too

Rate limiting down to the second

Important for high-traffic applications

Adding rules to an extended form request

It's all just arrays

What is the difference between the asset helpers?

And which one should you use?

What is the longest valid email address?

And how does this impact validation rules?

The life cycle of database hosting

Start simple and grow as needed

Preventing performance surprises with Laravel migrations

Keep your system healthy

Is there a performance penalty in MySQL for inserting a column?

The answer is nuanced

Passing arguments to a partial mock's constructor

You can't use Laravel's mock helper

Avoid unnecessary work when querying relationships

Keep your database happy

How would you test code that uses the `old()` helper?

And why you might want to

A better way to schedule commands

Avoid magic strings

Partial mocks don't run constructors

At least by default...

How to troubleshoot S3 filesystem issues

Silent failures are the worst
Book cover for A Little Bit of Laravel

Prefer a little bit more meat with your tips?

You're in luck! This ebook contains tips and tricks that are quick and easy to apply but make a noticeable improvement in any Laravel app. Dig in a little bit deeper with larger code samples and deeper explanations.

March, 2024


Understanding how caching works in GitHub Actions

Keep CI running fast

Are you really going to need that abstraction?

No really, are you sure?

Forcing all API requests to accept JSON

Avoid annoying errors

How to use Pusher with private encrypted channels

Important when dealing with sensitive data

An easy way to test UUIDs

Laravel has you covered

Understanding the order of authorization and model binding

And why you shouldn't just flip the order

A trick for geolocating US postal codes

It's dumb, but it works

How to step debug a queued job

For those times when dd is not enough

Cleaning up deprecated warnings during a database migration

Don't ignore it

Monitoring progress of a big database import

Without this, it feels like it takes forever

A good rule for when to create a factory state

Make your tests more readable

Should you change app code for a test?

It depends on why you're changing it

Loading a SQL backup without altering your configuration

Sometimes data is messy

Why I always add a primary key to many-to-many tables

Even when Laravel doesn't require it

Make sure your listeners are wired up

Without this simple test, nothing else matters

How to set CORS headers for built assets

And do it efficiently

Something to know if serving scripts for other sites

Especially if you moved to Vite

Loading relationships inside a queued job

Do it the Laravel way

A better way to drop a unique index

It involves less guesswork

Why pagination sometimes has overlapping pages

A deeper look inside the database

Sharing redirect logic between controller actions

Without resorting to messy hacks

February, 2024


What is the point of design patterns?

And have you fallen asleep yet?

Manual testing still has its place

Put yourself in the shoes of your users

Ignoring files per environment with Docker

And why you might want to do this

Quickly viewing test file structure

Another reason I like patterns

Picking a good test value

And avoid flaky tests

Pay attention to analytics

There can be some interesting insights

Consistent test organization

I like patterns

How we use migrations during early product development

When the schema changes quickly

Simple rules for writing authorization logic

A little bit of structure goes a long way

One place I avoid constructor property promotion

Just to keep the intent clear

Move that logic out of your view

How much is too much?

Alpha validation might not be doing what you think

It certainly surprised me the first time I bumped into it

Avoid changing your code purely for a test

Even if it's just to make a property public

Get better auto-complete from your factories

And reduce the need for @var docblocks

Watch out for this when testing Artisan commands

A weird reason for a failing test

A clean approach to testing events and listeners

Focus on what each thing should do

Another reason I like fillable instead of guarded

Every little bit helps

A safer way to check model strictness in production

Without annoying your users

Why does Laravel offer a max password length validation rule?

Doesn't hashing make this irrelevant?

Composer scripts and Valet isolate

Something to be aware of

Is it worth switching to another tool?

And how do you know when it's the right time?

January, 2024


Being explicit when something should not queue

A simple way to clearly communicate intent

Absolute paths and the Vite asset helper

Want to laugh at me? It's ok.

Don't become numb to pain in your project

You can make it better!

Why might you want to forget a route parameter?

And how does it work?

Inspecting files in a Docker container

And easily spotting what changed

Considerations when cleaning up a big table

Without causing downtime or making production slow

A measured approach to using Larastan

More isn't always better

How to test that a model wasn't changed

In a very simple way

Running HTTP requests right inside your editor

I promise I'm not getting paid by JetBrains

How we use seeders in our application

And why we rarely use them for testing

Another nice feature for local pull request review

It's even nicer than GitHub's UI

Eager loading, but only if you really need to

Since we want to reduce the number of queries

Route binding with two different models

While still leveraging the framework

Stop using $request->get()

There is a better way

Don't reinvent features that Laravel already has

For example, soft deletes

Vite Hot Module Replacement and setInterval

And a workaround if you really need one

What is the difference between boot and register in service providers

Where do I put this code?

Even GitHub comments have version control

And it occasionally helps you win arguments

Failing tests give you confidence

Not just knowing it fails, but why

Don't just get mad at some tech, dig in and learn

Once you learn it, then you can complain

Use factories to make your tests more readable

Only include relevant details in your test setup

One situation where it is useful to make more than one request in a test

Don't abuse this, but here's one case I think is okay

Using factories with more than one relationship

Sometimes you can't follow the typical naming convention

December, 2023


Redirect routes match all HTTP verbs

If you need to be more precise, here's a better way

Routes resolve differently when cached

But tests can help you catch the issue

The benefits of caching in CI

And it's not for performance reasons

Don't cache routes or views locally

Great for production, but trouble for development

View source always makes a new request

I forget this like once a year

How clear coding rules can free up your team

Have the debate once, then move on

Making a case for consistency

And the importance of a good README

Why the nullable validation rule is useful

Sometimes code is for your team, not just the PHP runtime

Why we don't use SQLite for tests

Faster isn't always better

When it might be ok to use a mixed type

It serves a useful purpose

Using writing to think through a problem

This has saved me many times when I felt stuck

Are you writing script definitions in Composer?

If not, we highly recommend it!

Why we don't use return types on controller actions

But we do like return types in general!

How to route based on the requested domain

Very useful if you serve multiple sites out of the same Laravel app

Keep your testing database separate from development

Understanding how phpunit.xml works opens up many possibilities

Don't rely on key ordering with MySQL JSON values

This behavior caught me by surprise

Different approaches to upgrading to a new major Laravel version

And we explain the pros and cons of each

Be consistent with composer scripts in CI

Keep it simple and save yourself some trouble

Why we like minimal seeders for tests

Getting this right will make your tests so much easier to maintain

You don't need to manually run seeders in your tests

Laravel is constantly making things nicer

A surprising issue with the Auth user and tests

Knowing how this works helps you avoid false failures in your tests

November, 2023


Lazy loading protection does not catch all N+1 queries

I discovered this one the hard way

Should you be able to override coding standards locally?

And why am I even asking this question?

A simple approach to localization using middleware

We used this on a recent project and it worked great.

How to rate limit by IP behind a Cloudflare proxy

If you don't do this, your app could actually start blocking Cloudflare traffic.

Don't take it personal if your contribution is rejected

You had a good idea and submitted it, but then the project rejects it. Now what?

Maybe you don't need to write code this time

And using a no-code tool may even give you more empathy for users of your app

How to reference a PHP CodeSniffer ruleset in EasyCodingStandard

This simple addition can make it much easier to migrate between tools

How does LetsEncrypt work behind Cloudflare?

If we use a proxy like Cloudflare, will it impact our ability to issue LetsEncrypt certificates?

Considerations when eager loading resource collections

Check out a subtle bug that avoided notice for a while, and how tests eventually helped us.

The right way to fix double encoding in Blade components

There is a lot of bad advice out there on this topic. Here's a better approach.

Version untracked files with PhpStorm

Another very handy use for PhpStorm's Local History feature

Enforcing Horizon auth in local

Is this overkill? Or does it give you more confidence in your application?

Cloudflare, Laravel, and TLS

Double check this setting to save yourself some trouble

A good rule around "down" migrations

Some would say don't bother to write "down" migrations. I argue that if you do, you should follow this rule.

Use the right tool for the job

You can still have your favorite, but it's good to see what else is out there

Should you manage roles and permissions with a UI?

It's tempting to let non-developers manage this data, but is it a good idea?

Understanding how Laravel sessions work

If we don't know how something works, it's more confusing when things behave differently than expected.

Using workarounds without creating a mess

Sometimes a workaround is unavoidable, but how can you avoid them from piling up technical debt?

Dealing with messy JSON

Sometimes API do unspeakable things and we have to work around it in our app

The value of a quick code demo

Sometimes it's more effective to show than tell

Don't get defensive during code review

Sometimes even a small observation can lead to a big improvement

How to assert a Laravel job is delayed

I've seen a lot of convoluted approaches to this, here's a drop-dead simple way to do it.

October, 2023


Ever need a different git setting per repo?

Maybe you have one git account for work, and one for open source. How do you make sure the right author is used in each repo?

Keep your code consistent

Ever find 5 different ways of doing something in a single codebase? Here's an approach to minimize that.

How to get rid of HEIC files in your app

And it makes your app easier to use for non-iPhone users too

The changing value of books throughout your career

Perhaps that book you thought was useless 5 years ago is exactly what you need today.

Actually read the whole error message

This seems really simple and obvious, but I've been burned in the past not doing this myself.

How much detail should you include in error messages?

Error messages are part of user experience.

Is it time to abstract this?

You spot two things in your app that are mostly the same. Is that an automatic refactor?

How do you test middleware?

Middleware can be a tricky thing to test. Here's what I did in a recent project.

Document before automating

As devs, sometimes we rush to coding a solution. But there's an important step that should come first.

A simple way to avoid multiple lookups within a request

Traditional caching comes with its own set of complexities. Here's a simple way to use basic PHP instead.

Joins in Eloquent? How?

Sometimes you need a join for performance or sorting by relations. Here's a way to do that without giving up Eloquent.

Ship something every day

There's value in shipping something, anything, at least once a day, even on a bigger feature.

Is sleep in a test always bad?

You might think adding sleep to a test is a dirty hack, but it's not always a bad thing.

Some reasons to write a down method in your migrations

And it's probably not the reason you think.

Use custom Mockery matchers for better Laravel assertions

Clean up your tests, especially when passing models as arguments

What does INT(11) mean in MySQL?

Here's a hint: it has nothing to do with length

How do you insert a new column at the beginning of a MySQL table?

And more importantly, why would you want to?

Be careful ordering columns in a migration

Laravel migrations let us insert a column in a specific position in a table, but it's not always a good idea.

Applying a class to a Markdown link

Ever wish you could add a CSS class to a Markdown link? Here's how I did it.

Don't forget to pin PECL extensions

We pin Composer packages and PHP versions, but what about PECL extensions?

How to make the AWS SDK much smaller

Only need a small portion of the AWS SDK, and wish you could make it a lot smaller?

Do you really need to install that SDK?

If you're integrating with a third-party API, you may not need to bring in a giant SDK.

September, 2023


Cache Larastan results in Github Actions

Larastan is a great tool, but it can slow down your CI pipeline. Here's how to cache the results to make each run faster.

Managing a micro-manager

It's frustrating to have a client or co-worker attempt to micro-manage you. We share some tips on how to make the working relationship better.

Drop unneeded steps in CI

This might seem pretty obvious, but it's easy to forget to remove steps from your CI pipeline when they are no longer needed.

Keep your Laravel CI pipeline running fast

CI pipelines tend to get slower over time, the more we add to them, and the bigger our project grows. Keeping an eye on this is important.

Knowing when to keep it simple

We take pride in building things "the right way", but there is a balance to strike with simplicity.

Should we write tests for this one-time command?

I'm a huge proponent of writing tests, but is there ever a valid time to skip them?

A good use case for the Eloquent sole() method

The sole() method has been around for a while, but I don't use it very much. Here's one place I found it useful.

The benefit of one-time commands

Is there any reason to build a Laravel command you only plan to use one time? Let's consider a few benefits.

Do we really need to build this feature?

Before diving into the code and cranking out a feature, sometimes it's useful to consider if we really need to build it at all.

Easily find specific code usages in PhpStorm without regex

Regexes in search logic are powerful, but this tip will supercharge your code searching without needing to get too fancy.

Understanding how Laravel view assertions work internally

No one likes a passing test when the actual logic is wrong. Here's how to make sure your view assertions are actually testing what you think they are.

Local history in PHPStorm

If you already have git, why would you use the local history feature in PHPStorm?

Be careful when using assertViewHas with null

Laravel's assertViewHas method doesn't work for null values. Here's how to test for them.

When to pull in top-level dependencies

If Laravel already requires a package, is there ever a reason to install it into your project directly?

Use GitHub issue templates to save time

Having a template not only makes issue creation easier, but also more consistent.

What if every Composer package had a fee?

An interesting thought experiment that might influence how many packages you pull into your project.

How to override PHPStorm file types

PHPStorm allows you to override the file type, giving you different syntax highlighting and auto-complete prompts.

When might you render a Blade template inline?

This isn't a feature I reach for frequently, but it's good to know it exists. Here is one use case where we found it quite handy.

How to make a bug fix way more valuable

Fixing bugs is important, but by taking just a little more time you can make your app much more resilient

Don't explicitly install dependencies if you don't need to

Pay attention to the dependencies Laravel or other packages are already bringing in. You may be pinning them to an older version without realizing it.

Take time to make things better

Look for opportunities to make things just a little bit nicer than you found them.

August, 2023


Tools should work for you, not boss you around

Ever have a day where you're "fixing the squiggles" in your editor and feel like the code got worse? Let's talk about how to avoid that.

Try not to make things worse

You know those parts of a codebase that feel "not great"? Be mindful to not further entrench those bad decisions.

Are fast queries always good?

I mean, yeah! Right? Well, there's more to the story...

Clean up dead git branch references automatically

When you merge a branch in GitHub, the reference to it is still in your local repo. How can you clean those up automatically?

A scenario using events and observers together

Sometimes it's useful to pair events and observers together. Let's look at one real-world example.

When should you use events and observers?

Laravel events and observers seem to do very similar things. So when would you use one over the other?

How do you know if something is slow in production?

The tools we used for finding slow code in development are not suitable for production. So how can we find slow code there?

Something slow? It's probably the database.

The absolute number one cause of slow code is the database. Laravel makes fixing some of the common issues very easy.

How do you know something is slow?

The first step in making your app faster is knowing what is slow. How do you figure that out?

What if you need a value that's random AND unique?

Sometimes we need random data that is ALSO unique. How can you generate that in Laravel?

Harness that post-conference excitement

New features! New packages! New techniques! How can we harness that excitement when we get back to work a conference?

Learn to love code review

Code review feedback might make us feel defensive, but if you approach it with the right attitude it can become something you value.

Find out what's slow BEFORE you deploy to prod

The earlier you find an issue, the better! How can we improve our chances of finding slow code before it reaches production?

Sometimes Eloquent can make a query slower

Eloquent improves developer efficiency, but in some cases it can make a query less efficient. Let's look at one example and how to fix it.
Don't miss out - get a tip like this in your inbox every weekday? Sign up below 👇🏼

Level up your Laravel skills!

Each 2-minute email has real-world advice you can use.