Understanding Laravel Collections: The unless Method Made Simple

Asfia AimanAsfia Aiman
3 min read

If you're new to PHP and Laravel, you might have heard about Laravel Collections. They provide an easy way to work with arrays of data. One of the helpful methods in Collections is unless. This method helps us run a piece of code only if a certain condition is false.

In this blog, we'll go step by step to understand how unless works in Laravel Collections, using simple examples.

What is the unless Method?

The unless method is like saying: "Do this, unless the condition is true." It runs the given function only when the condition is false.

Example:

Imagine we have a list of students, and we only want to add a new student if a condition is false.

use Illuminate\Support\Collection;

$students = collect(['Alice', 'Bob', 'Charlie']);

$students->unless(true, function (Collection $students) {
    return $students->push('David');
});

$students->unless(false, function (Collection $students) {
    return $students->push('Eve');
});

$students->all();

What happens here?

  • The first unless method has true as the condition, so it does nothing (David is NOT added).

  • The second unless method has false as the condition, so it adds Eve to the list.

Final output:

['Alice', 'Bob', 'Charlie', 'Eve']

Using unless with Two Actions

The unless method also allows two different actions:

  • The first action runs if the condition is false.

  • The second action runs if the condition is true.

Example:

Let's say we are managing a to-do list. If today is not a holiday, we add a task. Otherwise, we add a note saying, "Enjoy your day!"

$tasks = collect(['Buy groceries', 'Complete assignment']);

$isHoliday = true; // Change this to false to test

$tasks->unless($isHoliday, function (Collection $tasks) {
    return $tasks->push('Attend meeting');
}, function (Collection $tasks) {
    return $tasks->push('Enjoy your day!');
});

$tasks->all();

What happens here?

  • If $isHoliday is false, "Attend meeting" is added.

  • If $isHoliday is true, "Enjoy your day!" is added.

Final Output (if $isHoliday = true):

['Buy groceries', 'Complete assignment', 'Enjoy your day!']

When Should You Use unless?

The unless method is useful when:

  • You want to run a function only when a condition is false.

  • You want to avoid writing if (! $condition) { } in your code.

  • You need a cleaner, more readable way to handle conditions in Collections.

The Opposite of unless: The when Method

Laravel also provides the when method, which is the opposite of unless. It runs a function only if the condition is true.

Example:

If there is a discount available, we add a special price to the products list.

$products = collect(['Laptop', 'Phone']);

$hasDiscount = true;

$products->when($hasDiscount, function (Collection $products) {
    return $products->push('Special Offer: Tablet');
});

$products->all();

Final Output:

['Laptop', 'Phone', 'Special Offer: Tablet']

Summary

  • unless(condition, callback) → Runs the function only when the condition is false.

  • when(condition, callback) → Runs the function only when the condition is true.

By using unless and when, you can write cleaner and more readable code in Laravel.

Keep Practicing!

If you're new to Laravel, try experimenting with unless and when in your own projects. The more you use them, the easier they become to understand!

Happy coding!

0
Subscribe to my newsletter

Read articles from Asfia Aiman directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Asfia Aiman
Asfia Aiman

Hey Hashnode community! I'm Asfia Aiman, a seasoned web developer with three years of experience. My expertise includes HTML, CSS, JavaScript, jQuery, AJAX for front-end, PHP, Bootstrap, Laravel for back-end, and MySQL for databases. I prioritize client satisfaction, delivering tailor-made solutions with a focus on quality. Currently expanding my skills with Vue.js. Let's connect and explore how I can bring my passion and experience to your projects! Reach out to discuss collaborations or learn more about my skills. Excited to build something amazing together! If you like my blogs, buy me a coffee here https://www.buymeacoffee.com/asfiaaiman