2023-11-13 #php #laravel

Remove falsy values from a Laravel collection or array in PHP

The native array_filter() in PHP and collect()->filter() in Laravel also work without providing a filter callback.

array_filter([0, 1, '', 'a', false, true, []]);
// [1, 'a', true]
collect([0, 1, '', 'a', false, true, []])->filter();
// [1, 'a', true]

If you don't provide a callback, PHP will remove all empty values from the array.

2023-11-13 #laravel / rias.be

Adding stale while revalidate functionality to Laravel's cache

A stale while revalidate cache macro by Rias Van der Veken. With stale while revalidate, expired cache items will still be used when requested, but the data will be revalidated right after. That means the current request will be handled faster than if the cache would have to be revalidated, and the next request will receive fresh data.

Stale while revalidate is often used in web applications, popularized by Vercel's SWR React library.

2023-10-31 #laravel / stefanzweifel.dev

Stefan Zweifel on storing user preferences in a Laravel application

Stefan Zweifel explains how he stores user preferences in a Laravel application using spatie/laravel-data. With the data package, you can store user settings as a blog of JSON in your database—so you don't need to update your table schema for every change—and have a typed object to work with in code.

He mentions poor query performance as a possible tradeoff if you need to query the database for a specific value.

One thing to keep in mind is that querying for specific settings can lead to performance issues and should probably be avoided.

If your app regularly needs to query for users who have selected a particular date_format , it's better to promote this setting to its own column. This makes the work of your database and possible indexing much easier.

If this is something you need, you could solve it with a virtual column mapped to a JSON value of the settings object. There's a nice tutorial on that on the Kirschbaum blog

2023-09-28 #css #tailwind #laravel #blade

Empty states with CSS and the `:only-child` selector

Here's a quick trick with the :only-child selector to display an empty state in a list.

Read more

2023-07-27 #laravel #eloquent / janostlund.com

Comparing Eloquent's get, cursor, chunk, and lazy methods

A short & sweet overview of get(), cursor(), chunk(), and lazy() to retrieve models from the database. It's a tradeoff between speed and memory usage.

2023-07-21 #laravel #spatie

Some geeky frontend notes on the Full Stack Artisan website

At Laracon US we announced we're working on a new course at Spatie: Full Stack Artisan.

In Full Stack Artisan, we'll dive into building Laravel applications with Inertia, React, TypeScript, view models, our Laravel Data package, and more.

Last week I took a break from working on course content to set up branding and a landing page.

Read more

2023-06-16 #laravel

Laravel closure validation rules

Today I was looking for a way to create a custom Laravel validation rule without the overhead of a new class. The rule I needed would only be used in one place, so wanted to keep it close to (or in) the request class.

Upon re-reading the validation docs, I learned that Laravel supports closures as rules.

class JournalEntryRequest extends Request
public function rules(): array
return [
'lines' => [
function (string $attribute, mixed $value, Closure $fail) {
$debit = collect($value)->where('type', 'debit')->sum('amount');
$credit = collect($value)->where('type', 'credit')->sum('amount');
if ($debit !== $credit) {
$fail("Debit and credit don't match.");
if ($debit !== 0) {
$fail("Amount must be greater than 0.");

Just what I needed!

2023-04-19 #laravel

Comparing two database columns in Laravel

When you want to compare two database columns in Laravel, you can't use where because it treats the argument you're comparing to as a value.

Instead, Laravel has a whereColumn method you can use to compare a value to another column's value.

// Retrieve posts that were updated after
// they were published.
->whereColumn('updated_at', '>', 'published_at')

2023-04-05 #laravel

Eager load relations in Laravel using withWhereHas

When using whereHas in Laravel, it's not uncommon to also eager load the relation using with.

$posts = Post::query()
->whereHas('author', function (Builder $query) {
$query->where('name', 'Seb');

Laravel also has a more succinct method that combines the two: withWhereHas.

$posts = Post::query()
->withWhereHas('author', function (Builder $query) {
$query->where('name', 'Seb');

2023-03-17 #laravel #valet #php #nginx

PHP & NGINX logs with Laravel Valet

Putting this in a blog post because I always forget.

To view PHP logs from Laravel Valet:

open ~/.config/valet/Log/php-fpm.log

To view NGINX logs from Laravel Valet:

open ~/.config/valet/Log/nginx-error.log

2023-03-07 #laravel

Resolving a new instance of a singleton in Laravel

In Laravel, you can register a class as a singleton to always resolve the same object.

However, you might want to build another instance of the class. You could manually construct the class without Laravel's container, but if it has a bunch of dependencies it can be tedious.

With the build method, Laravel won't resolve a registered instance of the class, but build a new one with the container.

// AppServiceProvider::register()
// Resolve the singleton instance from the container
$mastodon = resolve(MastodonClient::class);
// Build a new instance
$anotherMastodon = app()->build(MastodonClient::class);

This can be useful when a Laravel package registers a class as a singleton but you need another instance.

2023-03-06 #php #laravel / freek.dev

How we keep our Laravel packages customizable at Spatie

Freek shares a few patterns we employ to let developers override behaviour in our packages.

One of the ways we keep maintenance burden low is by making our packages customizable. In this blog post, I'd like to cover some of our best tips to make a Laravel package easy to customize. Some of these tips will apply to regular projects as well.

2022-02-22 #laravel

Composable seeders in Laravel with callOnce

Laravel 9 is fresh out the door, and it contains a small contribution of mine: a new callOnce method for database seeders.

Read more

2022-01-10 #laravel #blade

Laravel Blade & View Models

An overview on view models in Laravel

Read more

2021-12-16 #mysql #laravel

Using MySQL `order by` while keeping one value at the end

The other day I needed to sort a dataset in MySQL and ensure one value was always at the end. I never fully understood how order by works, so I did some research on how to solve my problem and how order by behaves.

Read more