1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Huntie\JsonApi\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use DB; |
6
|
|
|
use Huntie\JsonApi\Support\JsonApiErrors; |
7
|
|
|
use Huntie\JsonApi\Http\JsonApiResponse; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Illuminate\Routing\Controller; |
11
|
|
|
|
12
|
|
|
abstract class JsonApiController extends Controller |
13
|
|
|
{ |
14
|
|
|
use JsonApiErrors; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Return the Eloquent Model for the resource. |
18
|
|
|
* |
19
|
|
|
* @return Model |
20
|
|
|
*/ |
21
|
|
|
abstract protected function getModel(); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Return the type name of the resource. |
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
protected function getModelType() |
29
|
|
|
{ |
30
|
|
|
return $this->getModel()->getTable(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Return a listing of the resource. |
35
|
|
|
* |
36
|
|
|
* @param Request $request |
37
|
|
|
* |
38
|
|
|
* @return JsonApiResponse |
39
|
|
|
*/ |
40
|
|
|
public function indexAction(Request $request) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$data = []; |
43
|
|
|
$records = $this->getModel()->all(); |
44
|
|
|
|
45
|
|
|
foreach ($records as $record) { |
46
|
|
|
$data[] = $this->transformRecord($record); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return new JsonApiResponse(compact('data')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return a specified record. |
54
|
|
|
* |
55
|
|
|
* @param Request $request |
56
|
|
|
* @param Model|int $record |
57
|
|
|
* |
58
|
|
|
* @return JsonApiResponse |
59
|
|
|
*/ |
60
|
|
|
public function showAction(Request $request, $record) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$record = $record instanceof Model ? $record : $this->findModelInstance($record); |
63
|
|
|
$data = $this->transformRecord($record); |
|
|
|
|
64
|
|
|
|
65
|
|
|
return new JsonApiResponse(compact('data')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Return an instance of the resource by primary key. |
70
|
|
|
* |
71
|
|
|
* @param mixed $key |
72
|
|
|
* |
73
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
74
|
|
|
* |
75
|
|
|
* @return Model|JsonApiResponse |
76
|
|
|
*/ |
77
|
|
|
protected function findModelInstance($key) |
78
|
|
|
{ |
79
|
|
|
return $this->getModel()->findOrFail($key); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Transform a model instance into a JSON API object. |
84
|
|
|
* |
85
|
|
|
* @param Model $record |
86
|
|
|
* @param array|null $include Relations to include |
87
|
|
|
* @param array|null $fields Field names of attributes to limit to |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
protected function transformRecord($record, array $include = [], array $fields = []) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$attributes = $record->toArray(); |
94
|
|
|
|
95
|
|
|
if ($fields) { |
|
|
|
|
96
|
|
|
$attributes = array_only($attributes, $fields); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return [ |
100
|
|
|
'type' => $record->getTable(), |
101
|
|
|
'id' => $record->id, |
102
|
|
|
'attributes' => array_except($attributes, ['id']), |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.