File size: 948 Bytes
5f32fbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php

namespace App\Providers;

use Illuminate\Routing\ResponseFactory;
use Illuminate\Support\ServiceProvider;

class MacrosServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->registerResponseFactoryMacro();
    }

    public function boot(): void
    {
        //
    }

    private function registerResponseFactoryMacro(): void
    {
        ResponseFactory::macro(
            'success',
            fn (string $message, array $data = []) => $this->json([
                'status'  => 'success',
                'message' => $message,
                'data'    => $data,
            ])
        );

        ResponseFactory::macro(
            'error',
            function (string $message, ?int $statusCode = 422) {
                return $this->json([
                    'status'  => 'error',
                    'message' => $message,
                ], $statusCode);
            });
    }
}