DEV Community

Cover image for 7 Laravel 12 Methods You Won’t Find Easily in the Docs
Tone Gabes
Tone Gabes

Posted on

7 Laravel 12 Methods You Won’t Find Easily in the Docs

Laravel 12 came with a lot of great features, but some of the best additions don’t show up in the documentation.

These “hidden” methods can make your code cleaner, faster, and more readable — if you know where to look.

Here are 7 lesser-known Laravel 12 helpers that deserve a place in your toolbox.👇

1. Collection::range(start, end, step)

Finally, you can generate ranges with a step:

collect()->range(1, 10, 2);
// [1, 3, 5, 7, 9]
Enter fullscreen mode Exit fullscreen mode

No more filter() gymnastics after a range(). This improves both clarity and performance when generating sequences.

2. Arr::partition()

Split an array into two groups based on a condition:

[$evens, $odds] = Arr::partition(
    [1, 2, 3, 4, 5],
    fn ($n) => $n % 2 === 0
);
Enter fullscreen mode Exit fullscreen mode

You get two clean arrays in one go. No need to convert to a collection just to split things.

3. TestResponse::ddJson() & ddBody()

Debugging test responses just got easier:

$this->get('/api/test')->ddJson('data');
Enter fullscreen mode Exit fullscreen mode

No more digging through getContent() and decoding JSON manually. These new methods let you inspect responses directly in your test flow.

4. Model::except([...])

Want to remove some fields from a model (like sensitive ones)?

return $user->except(['password']);
Enter fullscreen mode Exit fullscreen mode

Much cleaner than calling makeHidden() — especially in API responses where you only want to exclude a couple fields.

5. Model::fillAndInsert([...])

Need to bulk-insert records with model casts, timestamps, or UUIDs?

User::fillAndInsert([
    ['name' => 'Alice'],
    ['name' => 'Bob'],
]);
Enter fullscreen mode Exit fullscreen mode

It's insert() with all the benefits of model behavior. No hacks, just Eloquent.

6. Uri::of(...)->pathSegments()

Parse a URL and get its path segments cleanly:

Uri::of('https://site.com/2025/06')->pathSegments();
// ['2025', '06']
Enter fullscreen mode Exit fullscreen mode

No more explode() or string slicing. Just semantic URL handling.

7. Str::encrypt() & decrypt()

Want to encrypt a value in a fluent way?

str('secret')->encrypt()->prepend('x_');
Enter fullscreen mode Exit fullscreen mode

And then:

str($token)->after('x_')->decrypt();
Enter fullscreen mode Exit fullscreen mode

Simple, elegant encryption for small secrets or tokens — perfect for short-lived data.


Final Thoughts
Laravel 12 is full of gems — not just the ones in the docs.

By using these lesser-known helpers, you can:

✅ Write less boilerplate
✅ Reduce bugs
✅ Improve performance and readability

If you found this helpful, consider sharing it with your team or saving it for later.

Happy coding! 🚀

Top comments (0)