Support the ongoing development of →
posted 2 months ago

The answer is it depends. What I would advise you to do is to have an app/Support folder where you put these classes that have custom behaviour, and then you use these classes inside your Controllers or Commands.

I would advise to experiment! Try different folders, and refactor when you feel like that approach is too complicated to maintain. That way you will understand the pros/cons of everything you tried and will give you good opinions when starting a new project.

Btw, to calculate the cube root, you simply do pow($number, 1/3); . No need to create a class for it 😅


As @juampi92 said, there are many ways. Popular way is to add app/Support/Math.php or app/Helpers/Math.php where you will define your methods. Then autoload the file in composer.json:

"autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        "files": [

That way they will be available in all of your classes.

A more OOP approach is to define Traits. Again, you can create them in any directory you chose, I mainly use app/Traits, app/Models/Traits,... So, app/Traits/Math.php would look like:


namespace App\Traits;

trait Math
    protected function add(int $a, int $b): int
        return $a + $b;

Then your model would be:


namespace App\Models;

use App\Traits\Math;

class Addition extends Model
    use Math; // Extend class with trait


    public function add() : int
        return $this->add($this->a, $this->b);  // Method from trait
Last updated 1 month ago.

Sign in to participate in this thread!


Your banner here too?

Hugh Mungus hughcan Joined 30 Nov 2022


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2023 - All rights reserved.