#11 Embracing short closures, supercharged Eloquent tips, and more

Welcome to the 11th Andy's Web Dev Tips newsletter! Here's a few things from the last couple weeks:

A crash course on short closures in PHP

If you’re familiar with modern JavaScript at all, you may have come across short closures, also known as arrow functions, in your code. They’re a slick and simple notation that makes passing closures to functions straightforward and less cluttered.

PHP 7.4 introduced them, and they can really spruce up your backend code. Summarizing from this article on Stitcher, if you want to use short closures you’ll need to follow these guidelines:

  • Start with the fn() keyword

  • Have only a single expression, which acts as the return statement

  • Not include a return keyword

This example of a short closure in Laravel comes courtesy of @assertchris on Twitter.

5 tips for supercharged Laravel Eloquent queries

Last week I published a new video all about Laravel Eloquent queries. It’s pretty short, about 16 minutes long, and features five tips and tricks that you can use to simplify or enhance your Eloquent-powered methods in your Laravel apps.

My personal favorite is probably tip #2, where you can define a hasOne relationship alongside a hasMany one in your model class. When included in queries, it will only return a single latest instance of the same related model.

Flysystem V2 is on its way

Most developers that I’ve talked to agree that Flysystem is the de facto standard for libraries that enable working with files in PHP. If you’ve worked with file requests, storage, or retrieval in Laravel, then you’ve also had the pleasure of working with Flysystem.

Announced earlier this month, Flysystem V2 will be a massive overhaul and ground-up rewrite. More simplification in the API, better error handling, and an overall more sleek developer experience is what they’re going for. From the current V2 documentation, it’s an exciting direction they’re heading in, and definitely worth digging into if you’re already using a previous version of this package.

Creating a blog with Nuxt Content

For those who want to experiment with Nuxt, creating a blog is a fantastic introduction into this incredible framework. Not only does it introduce you to a lot of intricacies and techniques throughout the development process, but you could end up with a nice little side project or a re-built personal site at the end of it!

This in-depth tutorial from Debbie O’Brien combines the power of Nuxt with its Content module, enabling you to write plain Markdown files and retrieve them using a git-based flat-file API. This tutorial covers everything from installation of Nuxt and the Content module, to adding custom Vue components, to live-editing content.

Today I learned

This is a new section I’ll be adding with every email. It’ll include a small code snippet or helpful hint from the web development world.

You can use multiple -m flags when committing in git, and each one will create its own paragraph in the commit log. For example: git commit -m “Quick bug fixes” -m “Replacing a missing semicolon in the AppController”