Don't repeat response in controllers

3.8.2025

laravel
Don't repeat response in the controllers

Imagine you have a User controller and one of its methods is like this (just focus on the return part)

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function store(Request $request)
    {
        // validate request
        // save data
        // notification & email
        // ...

        return response()->json([
            'result' => 'success',
            'data' => $user,
        ], 200);
    }
}

And Something like that might repeat itself on other methods as well. In that case you have 2 options:

    • use base controller
    • use trait

How to use base controller

First off, what do I mean by base Controller ?

All of the controllers you create extend Controller:

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function store(Request $request)
    {
        // validate request
        // save data
        // notification & email
        // ...

        return response()->json([
            'result' => 'success',
            'data' => $user,
        ], 200);
    }
}

Now if you go to it’s definition using your favorite editor (i am using vscodevim and i set that to gd) you will open the /app/Http/Controller.php file which looks something like this:

namespace App\Http\Controllers;

abstract class Controller
{
    //
}

Inside this, you can basically write anything you want, but in this case, I am just interested of in responseOk

namespace App\Http\Controllers;

abstract class Controller
{
    public function responseOk($data)
    {
        return response()->json([
            'result' => 'success',
            'data' => $data
        ], 200);
    }
}

And then go back to your Controller and add this:

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function store(Request $request)
    {
        // validate request
        // save data
        // notification & email
        // ...

        return $this->responseOk($someData);
    }
}

Using trait

In Laravel, you have an artisan command for creating trait:

php artisan make:trait Traits/ResponseTrait

Then open the file and add the same method to it:

namespace App\Traits;

trait ResponseTrait
{
    public function responseOk($data)
    {
        return response()->json([
            'result' => 'success',
            'data' => $data
        ], 200);
    }
}

Then, go back to your controller and add the trait:

use Illuminate\Http\Request;
use App\Traits\ResponseTrait;

class UserController extends Controller
{
    use ResponseTrait;

    public function store(Request $request)
    {
        // validate request
        // save data
        // notification & email
        // ...

        return $this->responseOk($someData);
    }
}

You can also add this trait to your base controller:

namespace App\Http\Controllers;

use App\Traits\ResponseTrait;

abstract class Controller
{
    use ResponseTrait;
}

That’s pretty much it. As far as I know, the only real way to avoid repeating responses is by using the Action pattern , the Service pattern, or other similar architectural patterns.

Edit: Also Joel Clermont point out that you can also use Json Response as well.

If there is something I missed feel free to send DM on