Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JimNayzium's avatar

Beginner: Laravel Methods vs Native PHP Methods

Now that I am fully sold on Laravel and loving the deep dive I've taken the past six months, I find myself just instinctively changing and updating working code to Laravel methods instead of native PHP methods.

It feels a lot like the way I used to convert vanilla javascript things to JQuery, but then years later spent a lot of time taking most of the JQUERY out of projects I had worked on to make them work with things like React and Sveltekit etc...

When I see something like this in my project that maybe has been working for years as is, I find myself just stopping what I am doing and quickly converting it to Laravel Methods.


    $returnArray = array_values(

        array_filter(

            $data,

            // ** The injury_name_combo_3 is not in the rosters yet.
            function ($item, $key) use ($var1, $var2) {
                $searchTerm = strtolower($key);
                $strPos = strpos($searchTerm, $var1) !== false;

                $contains = $item['property'];

                if (in_array($contains, $var2)) {
                    if ($strPos === true) {
                        return true;
                    } else {
                        return $strPos ?: self::foundMatchProcess($item, $searchTerm);
                    }
                }
            },
            ARRAY_FILTER_USE_BOTH
        )
    );

    return $returnArray;

Gets refactored to this:


    $returnArray = collect($data)
        ->filter(function ($item, $key) use ($var1, $var2) {
            $searchTerm = str($key)->lower();
            $strPos = str($searchTerm)->contains($var1);
            $contains = $item['property'];

            if (collect($var2)->contains($contains)) {
                return $strPos ?: self::foundMatchProcess($item, $searchTerm);
            }

            return false;
        })
        ->values() // Reset array keys
        ->all();   // Convert back to an array if needed

    return $returnArray;


Do you think I will regret doing this in the future? šŸ˜„

Laravel feels like it's not akin to Jquery in terms of the bloat, but I am a novice at best so I truly don't know. All I know is Laravel has pretty much saved my life the past six months!

Is it worth the time and effort to refactor working code? I guess that's always up to me, but I am asking for your opinions and your modes of operation on how you go about things, just to help shape my perspective.

0 likes
3 replies
LaryAI's avatar
Level 58

Refactoring your code to use Laravel's collection methods can be beneficial for several reasons, but it's important to weigh the pros and cons based on your specific project needs and future maintainability. Here's a breakdown of the considerations:

Pros of Using Laravel Collection Methods

  1. Readability: Laravel's collection methods often make the code more readable and expressive. The chainable methods can make it easier to understand the flow of data transformations.

  2. Consistency: If your project is heavily based on Laravel, using its collection methods can provide consistency across your codebase, making it easier for other Laravel developers to understand and contribute.

  3. Functionality: Laravel collections offer a rich set of methods that can simplify complex operations, reducing the need for custom logic.

  4. Testing and Maintenance: Laravel's methods are well-tested and maintained by the community, which can reduce the likelihood of bugs compared to custom implementations.

Cons of Using Laravel Collection Methods

  1. Performance: In some cases, native PHP functions can be more performant than Laravel's collection methods, especially for very large datasets. However, for most applications, the difference is negligible.

  2. Dependency: Relying heavily on Laravel-specific methods can make it harder to decouple your code from the framework if you ever need to migrate to a different system.

  3. Learning Curve: For developers not familiar with Laravel, the collection methods might introduce a learning curve.

Conclusion

If your project is already using Laravel and you find the collection methods improve readability and maintainability, it's generally a good idea to refactor. However, if performance is a critical concern or if you anticipate needing to decouple from Laravel in the future, you might want to be more selective about where you apply these changes.

Ultimately, the decision to refactor should be based on your project's specific needs and your team's familiarity with Laravel. If the refactored code is more intuitive and aligns with your project's goals, it's likely a worthwhile investment.

Here's the refactored code using Laravel's collection methods for reference:

$returnArray = collect($data)
    ->filter(function ($item, $key) use ($var1, $var2) {
        $searchTerm = str($key)->lower();
        $strPos = str($searchTerm)->contains($var1);
        $contains = $item['property'];

        if (collect($var2)->contains($contains)) {
            return $strPos ?: self::foundMatchProcess($item, $searchTerm);
        }

        return false;
    })
    ->values() // Reset array keys
    ->all();   // Convert back to an array if needed

return $returnArray;

This approach leverages Laravel's collect, filter, contains, values, and all methods to achieve the same functionality as the native PHP code, with potentially improved readability and maintainability.

Sinnbeck's avatar

Laravel has been around for MANY years and has never been more popular I think. So no I dont think you will regret it any time soon (if ever). And even if you did, you could always take the collection code and add internally to your project. The collection version is without a doubt much much nice and easier to maintain

jlrdw's avatar

Just remember that laravel goes through the framework. The framework uses native php. I have learned a lot just by going through the framework in laravel and symfony to see how things are done.

And there are so many undocumented things as well.

Please or to participate in this conversation.