Laravel Code Tips

Make your Laravel code cleaner, faster, and safer.

MySQL has a blackhole store engine

180

πŸ”₯ MySQL has a blackhole store engine

MySQL has a blackhole store engine

πŸ‘¨β€πŸ”¬ Cool MySQL hack:

🧐 If your staging env has a table that ideally doesn't have data in it (esp production data)

πŸ•³ You can make it a BLACKHOLE table

πŸ˜… I used this to make sure a table with a list of emails to send never contains any rows. Because, you know....

Laravel's Http client supports concurrent requests using the `pool` method

179

πŸ”₯ Laravel's Http client supports concurrent requests using the `pool` method

Laravel's Http client supports concurrent requests using the `pool` method

πŸ”₯ #Laravel Tip: Need to make more than one HTTP request e.g. to one or more sites? You might think about running them concurrently to improve performance. The HTTP client's 'pool' method makes this a trivial exercise.

Interesting technique: add getters to Form Requests

177

πŸ”₯ Interesting technique: add getters to Form Requests

Interesting technique: add getters to Form Requests

πŸ’ͺPower up your #Laravel requests

You can add very simple getters on the request, so: βœ…You actually know what's on the request βœ…You have types βœ…You can do basic data transformation

Specify the columns you want to select when fetching all() models

176

πŸ”₯ Specify the columns you want to select when fetching all() models

Specify the columns you want to select when fetching all() models

🌢️ #Laravel tip: You can select the columns to run when using the Model::all() method. No need for an additional select() method.

A tiny tip to always keep in mind πŸ’‘

#PHP #Developers

You can name migrations "like this"

173

πŸ”₯ You can name migrations "like this"

You can name migrations "like this"

πŸ”₯ Epic Laravel Tip - Did you know that you don't necessarily have to use underscores for migrations names?

Make models prunable to periodically delete them from the DB

172

πŸ”₯ Make models prunable to periodically delete them from the DB

Make models prunable to periodically delete them from the DB

πŸ’‘#Laravel Eloquent Tip

Laravel 8 introduced the Prunable trait. You can use it to delete old models on a scheduled basis.

You don't need to write custom commands anymore:

You can add the context() and render() methods to custom exceptions

170

πŸ”₯ You can add the context() and render() methods to custom exceptions

You can add the context() and render() methods to custom exceptions

If you're creating custom exceptions inside your Laravel application, you can add a "context()" method to provide additional data to be printed in the log, as well as a "render()" method to allow the exception to display its own view to the user. Super cool! 🀩

Custom collections pair well with higher order proxies

164

πŸ”₯ Custom collections pair well with higher order proxies

Custom collections pair well with higher order proxies

πŸ”₯ Custom collections pair very well with higher order proxies

When you have some repetitive low level logic that you'd like to abstract into more readable methods that follow your business terminology, custom collections can be a great choice

PHP 8.1 enums can be used as model casts

163

πŸ”₯ PHP 8.1 enums can be used as model casts

PHP 8.1 enums can be used as model casts

πŸ”₯ Did you know that @laravelphp can cast model columns into instances an enum in PHP 8.1?

The example below will create an instance of OrderStatus based on variant value. If the status column has the value shipped, it will return the OrderStatus::Shipped variant.

You can use generic class-string annotations to hint that an instance of the passed class is returned

160

πŸ”₯ You can use generic class-string annotations to hint that an instance of the passed class is returned

You can use generic class-string annotations to hint that an instance of the passed class is returned

This is how you can hint that a function returns an instance of the class of which you passed the class name.

Cool stuff!

Hat tip to @enunomaduro

https://github.com/spatie/laravel-route-discovery/blob/7fa88922aa693b03b4763eda870867075ab44999/src/PendingRoutes/PendingRouteAction.php#L100-L107

Use partitions for deleting mass amounts of data

159

πŸ”₯ Use partitions for deleting mass amounts of data

Use partitions for deleting mass amounts of data

⚑ ️Database Tip

Some applications want to periodically deleted old historic data (or have to because of regulations). Deleting billions of rows is a very slow task which could take several minutes and may take your application down. Use partitions for efficient deleting!

You can supply default values for route parameters

158

πŸ”₯ You can supply default values for route parameters

You can supply default values for route parameters

πŸ”₯ Did you know that you can supply default values for route parameters in @laravelphp?

In the screenshot below, we use the current user's account as the default value for {account}. This means we don't have to provide it when generating the users.index route.

You can use `onerror` on the image tag

156

πŸ”₯ You can use `onerror` on the image tag

You can use `onerror` on the image tag

😲 TIL: You can hide an image when it is broken so you don't get that ugly broken image icon from the browser.

Use skip() when writing tests ahead of time

154

πŸ”₯ Use skip() when writing tests ahead of time

Use skip() when writing tests ahead of time

🌢️ #Laravel #pestphp tip

If you like to plan your tests ahead of time and implement them one by one, you can always make use of a handy method skip() to tell the test runner to ignore those empty tests.

skip() accepts a callback too!

Number markdown lists with `1.` rather than the actual number

153

πŸ”₯ Number markdown lists with `1.` rather than the actual number

Number markdown lists with `1.` rather than the actual number

πŸ’‘ A little thing I always like to do. Use only "1." for numbered lists in markdown. The list is rendered the same, but adding an item doesn't require you to renumber the other items 🀘

You can use first class callables for routes in PHP 8.1

152

πŸ”₯ You can use first class callables for routes in PHP 8.1

You can use first class callables for routes in PHP 8.1

πŸ”₯ You can use first class callables for routes in PHP 8.1

The caveat is that the methods need to be static because there's no syntax for referencing instance methods on classes

I don't use constructor DI in my apps, so static methods are fine despite feeling a bit non-standard

You can add a --refresh flag to the artisan down command to send a refresh header to the browser

150

πŸ”₯ You can add a --refresh flag to the artisan down command to send a refresh header to the browser

You can add a --refresh flag to the artisan down command to send a refresh header to the browser

#WeirdDevReadsDocs

Did you know?

You can add a --refresh flag to the artisan down command to send a refresh header to the browser. I think that's helpful for well visited apps where you trying to keep perceived downtime as low as possible!

https://laravel.com/docs/8.x/configuration#maintenance-mode

@laravelphp

Use match () or switch () statements to conditionally scope database queries based on query strings in the request

148

πŸ”₯ Use match () or switch () statements to conditionally scope database queries based on query strings in the request

Use match () or switch () statements to conditionally scope database queries based on query strings in the request

Remembered this great tip recently, and it's nice to see how fast PHP evolves

The exact same behavior can be implemented using match () with a default value, which also removes the need for the second callback ⚑️ https://twitter.com/reinink/status/1233017064492761088

Adding WHERE scopes which use an indexed column to a query that's primarily filtered by another column is a great way to speed up DB performance

147

πŸ”₯ Adding WHERE scopes which use an indexed column to a query that's primarily filtered by another column is a great way to speed up DB performance

Adding WHERE scopes which use an indexed column to a query that's primarily filtered by another column is a great way to speed up DB performance

⚑️ Database Tip

Sometimes you have seldom used queries and you can't add an index on columns for these queries. A nice trick is to use another indexed condition so the rows are filtered by the indexable column first and afterwards again for the more limiting condition.

You can use the array spread syntax to pass an array of arguments to another function

146

πŸ”₯ You can use the array spread syntax to pass an array of arguments to another function

You can use the array spread syntax to pass an array of arguments to another function

πŸ”₯ You can use array spreading to pass an array as arguments to another function. #php

Here’s a self-contained example where we spread the array output of sys_getloadavg. That function returns an array with three elements.

https://github.com/spatie/cpu-load-health-check/blob/70f387d5167c24d4000d825a5bddab2504d557fb/src/CpuLoad.php#L17

#php

You can use $request->whenFilled() to execute some logic only when a specific value is part of the request data

142

πŸ”₯ You can use $request->whenFilled() to execute some logic only when a specific value is part of the request data

You can use $request->whenFilled() to execute some logic only when a specific value is part of the request data

πŸ”₯Another amazing #Laravel feature!πŸ”₯

We often write if statements to check if a value is present on a request or not.

You can simplify it with the whenFilled() helper.

πŸ‘‹If you find this useful, consider clicking the button that says Retweet. It's a very cool button!

Writing simple "page can be visited" tests is a great way to get started with testing

141

πŸ”₯ Writing simple "page can be visited" tests is a great way to get started with testing

Writing simple "page can be visited" tests is a great way to get started with testing

Does your @laravelphp application have 0 tests? Here is one test you can easily add to get started. It's probably the most important test β€” in web projects β€” and it has an enormous value.

βœ“ Ensures your application boots. βœ… βœ“ Ensures the home page can be loaded. πŸ’¨

You can calculate multiple aggregates in a single database query

139

πŸ”₯ You can calculate multiple aggregates in a single database query

You can calculate multiple aggregates in a single database query

⚑️ Database Tip

You don't have to execute multiple queries to calculate different aggregates. With the filter clause you can narrow the rows which should be included for the calculation. So you may need to scan the table only once, the performance impact can be massive πŸ”₯

Use typehints wherever possible to improve code safety and IDE support

138

πŸ”₯ Use typehints wherever possible to improve code safety and IDE support

Use typehints wherever possible to improve code safety and IDE support

πŸ”₯ Simple typehints and annotations go a long way

Even without any docblocks, this code is perfectly understandable by both developers and IDEs

Writing code like this: πŸ‘‰ Prevents bugs using type safety πŸ‘‰ Makes the code self-documenting πŸ‘‰ Takes only a few seconds to implement

You can pass parameters to Laravel middleware classes

128

πŸ”₯ You can pass parameters to Laravel middleware classes

You can pass parameters to Laravel middleware classes

πŸ”₯ Remember that you can pass parameters to your custom @laravelphp middleware classes

This simple middleware allows me to inject onboarding screens in an Inertia app.

You can validate user input inside middleware

127

πŸ”₯ You can validate user input inside middleware

You can validate user input inside middleware

βœ” You can validate fields even within a middleware in Laravel.

This would help when you need to constantly validate the same field(s) on your requests:

Use aggregate methods to count related models

124

πŸ”₯ Use aggregate methods to count related models

Use aggregate methods to count related models

When you're counting related models in @laravelphp, opt for aggregates!

πŸ‘Ž Using the count() method on a collection of related models is a tad slower.

You can implement the ShouldBeEncrypted interface on your jobs to encrypt their payload before they get pushed to the queue

123

πŸ”₯ You can implement the ShouldBeEncrypted interface on your jobs to encrypt their payload before they get pushed to the queue

You can implement the ShouldBeEncrypted interface on your jobs to encrypt their payload before they get pushed to the queue

πŸ’‘ Since @laravelphp v8.19, there is a ShouldBeEncrypted interface that jobs can implement. This encrypts the payload, for example, in case the properties hold sensitive data.

As of v8.25, this works for Listeners, Mailables, and Notifications as well πŸ’«

#Laravel #PHP #webdev https://twitter.com/pascalbaljet/status/1462711618329055232

You can convert Eloquent Collections back to a Query Builder instance

122

πŸ”₯ You can convert Eloquent Collections back to a Query Builder instance

You can convert Eloquent Collections back to a Query Builder instance

A tiny tip about #laravel eloquent.

To retrieve the Query Builder after filtering the results: you can use ->toQuery().

The method internally use the first model of the collection and a whereKey comparison on the Collection models.

Use prefetching to boost the perceived speed of Livewire apps

121

πŸ”₯ Use prefetching to boost the perceived speed of Livewire apps

Use prefetching to boost the perceived speed of Livewire apps

To boost perceived speed in your @LaravelLivewire apps when loading data, make use of prefetching.

The .prefetch modifier tells Livewire to fetch the result in the background on hover, and then displays the result when the button is actually clicked ✨

Create renderable exceptions

117

πŸ”₯ Create renderable exceptions

Create renderable exceptions

Laravel Tip: You can customize how your exceptions are rendered by adding a 'render' method to your exception.

For example, this allows you to return JSON instead of a Blade view when the request expects JSON.

You can use the data_get() method to safely access a nested array value

114

πŸ”₯ You can use the data_get() method to safely access a nested array value

You can use the data_get() method to safely access a nested array value

πŸ’‘#Laravel Tip: Dealing with deeply-nested arrays can result in missing key / value exceptions. Fortunately, Laravel's data_get() helper makes this easy to avoid. It also supports deeply-nested objects.

Including a unique value, like now() or Str::random(), in the Blade template of a Livewire component is a great way to check if that part of the DOM is getting re-rendered

113

πŸ”₯ Including a unique value, like now() or Str::random(), in the Blade template of a Livewire component is a great way to check if that part of the DOM is getting re-rendered

Including a unique value, like now() or Str::random(), in the Blade template of a Livewire component is a great way to check if that part of the DOM is getting re-rendered

A quick and easy way to check if @LaravelLivewire components are re-rendering when you don't want/need them to.

Add now() or now()->timestamp to your component template. The time value will increment if there's a re-render side effect!

You can use the form="another-form" attribute on buttons to submit a different form

112

πŸ”₯ You can use the form="another-form" attribute on buttons to submit a different form

You can use the form="another-form" attribute on buttons to submit a different form

πŸ”₯ I sometimes forget you can use form="form-name" on input types to hook fields inside one form to another. This makes it as if those fields were inside that <form>-tag.

This is super useful if the fields are rendered in the wrong form, but UI wise it makes sense.

Use the *quietly() methods to create models without firing events using your factories

109

πŸ”₯ Use the *quietly() methods to create models without firing events using your factories

Use the *quietly() methods to create models without firing events using your factories

πŸ“ Here's part 2 of the new Database and Eloquent ORM features in @laravelphp 8.x since the release in September 2020.

One of my favorites is the ability in Model Factories to create models without dispatching any events πŸ”₯

Blog post: https://protone.media/en/blog/database-and-eloquent-orm-new-features-and-improvements-since-the-original-laravel-8-release-22

#Laravel #PHP

Use the --requests flag when creating models to also create form requests

106

πŸ”₯ Use the --requests flag when creating models to also create form requests

Use the --requests flag when creating models to also create form requests

This release of Laravel also includes a "--requests" flag when making models via Artisan. This option will create a "Store{Model}Request" and "Update{Model}Request" form request + update the controller to use those requests on their respective methods. πŸ”₯

Thanks @PovilasKorop!

Use Common Table Expressions to refine your data in complex Eloquent queries

104

πŸ”₯ Use Common Table Expressions to refine your data in complex Eloquent queries

Use Common Table Expressions to refine your data in complex Eloquent queries

⚑️ Database Tip

Sometimes you have very complex queries involving multiple calculation steps. Instead of nesting queries or doing it in application code you can use Common Table Expressions (CTE) to refine your data in every step and even reuse former steps.

Use the Str::headline() method to convert strings to studly words

103

πŸ”₯ Use the Str::headline() method to convert strings to studly words

Use the Str::headline() method to convert strings to studly words

πŸ’‘It's refactor time!

Laravel 8.65 introduces the Str::headline() method, this method converts a string into studly words!

A small change like this results in less noise in my code.

Thanks @SteveTheBauman for this implementation! πŸ™Œ

Use the withAggregate() method to add related values to Eloquent queries using subselects

99

πŸ”₯ Use the withAggregate() method to add related values to Eloquent queries using subselects

Use the withAggregate() method to add related values to Eloquent queries using subselects

πŸ’‘ A little #Laravel tip from the latest blog post to get the week started!

Under the hood, the withAvg/withCount/withSum and other methods in Eloquent use the 'withAggregate' method. You can use this method to add a subselect based on a relationship πŸ’«

#PHP #webdev #OpenSource

You can create text-based tables in Laravel even outside CLI commands

96

πŸ”₯ You can create text-based tables in Laravel even outside CLI commands

You can create text-based tables in Laravel even outside CLI commands

If you ever need to generate text-based tables for sending in text messages, outputting in console, or in logs, @symfony provides an awesome "Table" class, just for that! πŸ’… Such a cool utility! 🀩

You can use valueOrFail() to fetch a value from the database, or abort the request if the queried record does not exist

92

πŸ”₯ You can use valueOrFail() to fetch a value from the database, or abort the request if the queried record does not exist

You can use valueOrFail() to fetch a value from the database, or abort the request if the queried record does not exist

In the next blog posts, I'll cover Database and Eloquent improvements and new features in #Laravel 8.x.

Here's a great little feature that got added in v8.60 πŸ”₯

PR: https://github.com/laravel/framework/pull/38707

Yesterday's post about Collections: https://protone.media/en/blog/a-look-at-new-features-and-improvements-since-the-original-laravel-80-release-collections

#PHP #webdev #opensource

Use declare(strict_types=1) to enable strict mode for type checking

87

πŸ”₯ Use declare(strict_types=1) to enable strict mode for type checking

Use declare(strict_types=1) to enable strict mode for type checking

PHP reminder: The "declare(strict_types = 1);" directive enables strict mode. In strict mode, only a variable of exact type of the β€œtype declaration” will be accepted, or a "TypeError" will be thrown. βœ…

You can make the Http client throw exceptions to terminate execution on any received errors

80

πŸ”₯ You can make the Http client throw exceptions to terminate execution on any received errors

You can make the Http client throw exceptions to terminate execution on any received errors

πŸ’‘ By default, the @laravelphp HTTP client doesn't throw exceptions on client and server errors.

However, you can make it do that by using the 'throw' method, and this method is chainable πŸ”₯

Documentation: https://laravel.com/docs/8.x/http-client#throwing-exceptions

#Laravel #PHP #webdev #webdevelopment

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.

Avoid queries in Blade when possible

36

πŸ”₯ 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.

Create custom Blade directives for business logic

35

πŸ”₯ 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

34

πŸ”₯ 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

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.

Don't use a controller namespace

30

πŸ”₯ 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.

Consider single-action controllers

29

πŸ”₯ 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.

Use custom config files

28

πŸ”₯ 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.

Don't use model methods to retrieve data

27

πŸ”₯ 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.

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.

Import namespaces instead of using aliases

25

πŸ”₯ 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 query scopes for complex where()s

24

πŸ”₯ 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.

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

Don't use abbreviations

21

πŸ”₯ 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

Use custom collections

20

πŸ”₯ 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.

Use Data Transfer Objects (DTOs)

19

πŸ”₯ 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.

Create fluent objects

18

πŸ”₯ 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.

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.

Extract methods

13

πŸ”₯ 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

12

πŸ”₯ 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

Use events

11

πŸ”₯ 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

Create model methods for business logic

10

πŸ”₯ 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 action classes

9

πŸ”₯ 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 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.

We're writing a book, you can get it for free here.