Laravel Code Tips

Make your Laravel code cleaner, faster, and safer.

Readonly properties

77

🔥 Readonly properties

Readonly properties

They're a great way to make your components safer and faster.

Say you have a component for editing some resource.

You enforce the ACL in mount().

But if the user could change the currently edited resource, you'd need to enforce ACL on all requests!

Replace children with a custom directive

76

🔥 Replace children with a custom directive

Replace children with a custom directive

Imagine that you have a nested component that depends on the parent state. You don't need to sync child state into parent, you just want to replace the child when the parent state changes.

You can use my custom directive for that.

Quick tip: Use $wire.entangle() instead of @entangle

74

🔥 Quick tip: Use $wire.entangle() instead of @entangle

Quick tip: Use $wire.entangle() instead of @entangle

It's good to get into the habit of using $wire, since it provides a full communication layer for your Livewire component.

It also doesn't run into issues when you use it inside single quoted attributes.

Advanced notifications

73

🔥 Advanced notifications

Advanced notifications

This trait allows dispatching notifications:

  • on the current page
  • on the next page (after redirect)
  • from any part of your code: you can run Lean::notify() in an action class, a model method, or anywhere else — and it will be sent to the browser
Advanced trait design

72

🔥 Advanced trait design

Advanced trait design

Let's look at the same trait again. It does two interesting things.

  1. Livewire::listen() is superior to hydrate* and dehydrate* when you need access to specific parts of the lifecycle

  2. The instanceof self check scopes the listener to the component

Custom response effects

71

🔥 Custom response effects

Custom response effects

You can add custom data to response effects. Those are separate from component data, and act more like events.

You can use them when you have some JS code that looks at Livewire responses, and when you want more control than dispatching browser events

Compose components using traits

70

🔥 Compose components using traits

Compose components using traits

Traits are a powerful way to reuse functionality between Livewire components.

They're generally better than component nesting, since that comes with more complexity and worse performance.

Next up: Advanced trait examples

Call methods from JavaScript

69

🔥 Call methods from JavaScript

Call methods from JavaScript

Livewire's properties are extremely powerful, which often makes us forget that we can also call methods from the frontend.

Livewire gives you a full component API. Use it!

Use wire:replace

68

🔥 Use wire:replace

Use wire:replace

As you know, Livewire sometimes runs into painful DOM diffing issues. They can usually be solved with simple workarounds, but sometimes those workarounds get quite complex.

So I built a package that adds a wire:replace directive, telling Livewire to replace an entire chunk of the DOM, instead of trying to diff individual changes.

This one is less obscure

65

🔥 This one is less obscure

This one is less obscure

The $loop variable.

It greatly complements Tailwind-styled tables.

Striped tables done easily 🎉

Laravel migrations have a very nice syntax for foreign keys

60

🔥 Laravel migrations have a very nice syntax for foreign keys

I didn't know about this for so long.

Instead of: $table->unsignedBigInteger('user_id'); $table->foreign('user_id')->references('id')->on('users');

You do: $table->foreignId('user_id')->constrained();

Comparing changes on 'saving' using:

56

🔥 Comparing changes on 'saving' using:

Comparing changes on 'saving' using:

$model->getOriginal()

in Eloquent event listeners can be very useful.

One such use case is checking whether an order is locked. If it is, the only write action we permit is unlocking it. Only then can changes be made.

Laravel Telescope is great for so many things

52

🔥 Laravel Telescope is great for so many things

Laravel Telescope is great for so many things

One of them is examining sent emails ✉️

You usually don't need a service like Mailtrap. Just use the log mail driver and install Telescope.

(image stolen from the internet)

Relevant for people building packages 📦

50

🔥 Relevant for people building packages 📦

Relevant for people building packages 📦

It's IMO better to use static properties and have "self-contained" behavior & configuration. Than to have a billion config keys. Especially when building large packages.

Makes the package more extensible, by consisting of atomic parts

People underutilize the exception handler

44

🔥 People underutilize the exception handler

People underutilize the exception handler

You can tell the app what response to return when a specific exception is encountered.

This is the simplest example. There's many more use cases.

Write functional code when it benefits you

41

🔥 Write functional code when it benefits you

Write functional code when it benefits you

Functional code can both clean things up and make them impossible to understand. Refactor common loops into functional calls, but don't write stupidly complex reduce()s just to avoid writing a loop. There's a use case for both.

Context matters

40

🔥 Context matters

Context matters

Above I said that moving business logic to action/service classes is good. But context matters

Here's code design advice from a popular "Laravel best practices" repo. There's absolutely no reason to put a 3-line check into a class. That's just overengineered

Use collections when they can clean up your code

39

🔥 Use collections when they can clean up your code

Use collections when they can clean up your code

Don't turn all arrays into collections just because Laravel offers them, but DO turn arrays into collections when you can make use of collection syntax to clean up your code.

Use docblocks only when they clarify things

38

🔥 Use docblocks only when they clarify things

Use docblocks only when they clarify things

Many people will disagree with this, because they do it. But it makes no sense.

There's no point in using docblocks when they don't give any extra information. If the typehint is enough, don't add a docblock.

That's just noise.

Create custom Blade directives for business logic

36

🔥 Create custom Blade directives for business logic

Create custom Blade directives for business logic

You can make your Blade templates more expressive by creating custom directives. For example, rather than checking if the user has the admin role, you could use @admin.

Use strict comparison

35

🔥 Use strict comparison

Use strict comparison

ALWAYS use strict comparison (=== and !==). If needed, cast things go the correct type before comparing. Better than weird == results

Also consider enabling strict types in your code. This will prevent passing variables of wrong data types to functions

Avoid queries in Blade when possible

34

🔥 Avoid queries in Blade when possible

Avoid queries in Blade when possible

Sometimes you may want to execute DB queries in blade. There are some ok use cases for this, such as in layout files.

But if it's a view returned by a controller, pass the data in the view data instead.

Consider using helpers instead of facades. They can clean things up

33

🔥 Consider using helpers instead of facades. They can clean things up

Consider using helpers instead of facades. They can clean things up

This is largely a matter of personal preference, but calling a global function instead of having to import a class and statically call a method feels nicer to me.

Bonus points for session('key') syntax.

Use short operators

32

🔥 Use short operators

Use short operators

PHP has many great operators that can replace ugly if checks. Memorize them.

Be friends with your IDE

31

🔥 Be friends with your IDE

Be friends with your IDE

Install extensions, write annotations, use typehints. Your IDE will help you with getting your code working correctly, which lets you spend more energy on writing code that's also readable.

Consider single-action controllers

30

🔥 Consider single-action controllers

Consider single-action controllers

If you have a complex route action, consider moving it to a separate controller.

For OrderController::create, you'd create CreateOrderController.

Another solution is to move that logic to an action class — do what works best in your case.

Don't use a controller namespace

29

🔥 Don't use a controller namespace

Don't use a controller namespace

Instead of writing controller actions like [email protected], use the callable array syntax [PostController::class, 'index'].

You will be able to navigate to the class by clicking PostController.

Don't use model methods to retrieve data

28

🔥 Don't use model methods to retrieve data

Don't use model methods to retrieve data

If you want to retrieve some data from a model, create an accessor.

Keep methods for things that change the model in some way.

Use custom config files

27

🔥 Use custom config files

Use custom config files

You can store things like "results per page" in config files. Don't add them to the app config file though. Create your own. In my e-commerce project, I use config/shop.php.

Create single-use Blade includes

26

🔥 Create single-use Blade includes

Similar to single-use traits.

This tactic is great when you have a very long template and you want to make it more manageable.

There's nothing wrong with @including headers and footers in layouts, or things like complex forms in page views.

Create query scopes for complex where()s

25

🔥 Create query scopes for complex where()s

Create query scopes for complex where()s

Rather than writing complex where() clauses, create query scopes with expressive names.

This will make your e.g. controllers have to know less about the database structure and your code will be cleaner.

Import namespaces instead of using aliases

24

🔥 Import namespaces instead of using aliases

Import namespaces instead of using aliases

Sometimes you may have multiple classes with the same name. Rather than importing them with an alias, import the namespaces.

Create single-use traits

23

🔥 Create single-use traits

Create single-use traits

Adding methods to classes where they belong is cleaner than creating action classes for everything, but it can make the classes grow big

Consider using traits. They're meant primarily for code reuse, but there's nothing wrong with single-use traits

Use custom collections

21

🔥 Use custom collections

Use custom collections

Creating custom collections can be a great way to achieve more expressive syntax. Consider this example with order totals.

Don't use abbreviations

20

🔥 Don't use abbreviations

Don't use abbreviations

Don't think that long variable/method names are wrong. They're not. They're expressive.

Better to call a longer method than a short one and check the docblock to understand what it does

Same with variables. Don't use nonsense 3-letters abbreviations

Create fluent objects

19

🔥 Create fluent objects

Create fluent objects

You can also create objects with fluent APIs. Gradually add data by with separate calls, and only require the absolute minimum in the constructor.

Each method will return $this, so you can stop at any call.

Use Data Transfer Objects (DTOs)

18

🔥 Use Data Transfer Objects (DTOs)

Use Data Transfer Objects (DTOs)

Rather than passing a huge amount of arguments in a specific order, consider creating an object with properties to store this data.

Bonus points if you can find that some behavior can be moved into to this object.

Don't just write procedural code in classes

17

🔥 Don't just write procedural code in classes

Don't just write procedural code in classes

This ties the previous tweet with the other tips here. OOP exists to make your code more readable, use it. Don't just write 400 line long procedural code in controller actions.

Here's code from my first Laravel project 😬

Dedicate a weekend towards learning proper OOP

16

🔥 Dedicate a weekend towards learning proper OOP

Know the difference between static/instance methods & variables and private/protected/public visibility. Also learn how Laravel uses magic methods.

You don't need this as a beginner, but as your code grows, it's crucial.

Create helper functions

15

🔥 Create helper functions

Create helper functions

If you repeat some code a lot, consider if extracting it to a helper function would make the code cleaner.

Avoid helper *classes*

14

🔥 Avoid helper *classes*

Avoid helper *classes*

Sometimes people put helpers into a class.

Beware, it can get messy. This is a class with only static methods used as helper functions. It's usually better to put these methods into classes with related logic or just keep them as global functions.

Use events

13

🔥 Use events

Use events

Consider offloading some logic from controllers to events. For example, when creating models.

The benefit is that creating these models will work the same everywhere (controllers, jobs, ...) and the controller has one less worry about the details of the DB schema

Extract methods

12

🔥 Extract methods

Extract methods

If some method is too long or complex, and it's hard to understand what exactly is happening, split the logic into multiple methods.

Consider using form requests

11

🔥 Consider using form requests

Consider using form requests

They're a great place to hide complex validation logic.

But beware of exactly that — hiding things. When your validation logic is simple, there's nothing wrong with doing it in the controller. Moving it to a form request makes it less explicit

Create action classes

10

🔥 Create action classes

Create action classes

Let's expand on the previous example. Sometimes, creating a class for a single action can clean things up.

Models should encapsulate the business logic related to them, but they shouldn't be too big.

Create model methods for business logic

9

🔥 Create model methods for business logic

Create model methods for business logic

Your controllers should be simple. They should say things like "create invoice for order". They shouldn't be concerned with the details of how your database is structured.

Leave that to the model.

Create variables when they improve readability

8

🔥 Create variables when they improve readability

Create variables when they improve readability

The opposite of the previous tip. Sometimes the value comes from a complex call and as such, creating a variable improves readability & removes the need for a comment.

Remember that context matters & your end goal is readability

It's about the *micro*

3

🔥 It's about the *micro*

Using some "macro" philosophy for structuring your code, like hexagonal architecture or DDD won't save you.

A clean codebase is the result of constant good decisions at the micro level.