File tree 1 file changed +45
-0
lines changed 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace App \Traits ;
4
+
5
+ use Illuminate \Http \JsonResponse ;
6
+ use App \Enums \StatusCode ;
7
+
8
+ trait HttpResponses
9
+ {
10
+
11
+ /**
12
+ * Returns as success with json response
13
+ *
14
+ * @param mixed $data
15
+ * @param string $message
16
+ * @param int $statusCode
17
+ * @return JsonResponse
18
+ */
19
+ public function success (mixed $ data , string $ message = 'okay ' , int $ statusCode = StatusCode::Ok->value ): JsonResponse
20
+ {
21
+ return response ()->json ([
22
+ 'status ' => $ statusCode ,
23
+ 'success ' => true ,
24
+ 'message ' => $ message ,
25
+ 'data ' => $ data ,
26
+ ], $ statusCode );
27
+ }
28
+
29
+ /**
30
+ * Returns as success with json response
31
+ *
32
+ * @param string $message
33
+ * @param int $statusCode
34
+ * @return JsonResponse
35
+ */
36
+ public function error (string $ message , int $ statusCode = StatusCode::BadRequest->value , mixed $ data = null ): JsonResponse
37
+ {
38
+ return response ()->json ([
39
+ 'status ' => $ statusCode ,
40
+ 'success ' => false ,
41
+ 'message ' => $ message ,
42
+ 'data ' => $ data ,
43
+ ], $ statusCode );
44
+ }
45
+ }
You can’t perform that action at this time.
0 commit comments