1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoeSangue\Http\Controllers\API; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use DoeSangue\Http\Requests\CreatePostRequest; |
7
|
|
|
use DoeSangue\Http\Requests\UpdatePostRequest; |
8
|
|
|
use DoeSangue\Http\Controllers\Controller; |
9
|
|
|
use DoeSangue\Models\Post; |
10
|
|
|
|
11
|
|
|
class PostsController extends Controller |
12
|
|
|
{ |
13
|
|
|
public function __construct() |
14
|
|
|
{ |
15
|
|
|
$this->middleware('jwt.auth', ['except' => ['index', 'show']]); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function index() |
19
|
|
|
{ |
20
|
|
|
$posts = Post::all(); |
21
|
|
|
|
22
|
|
|
return response()->json(compact('posts')); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function show($id) |
26
|
|
|
{ |
27
|
|
|
$post = Post::find($id); |
28
|
|
|
|
29
|
|
|
if (!$post) { |
30
|
|
|
return response()->json( |
31
|
|
|
[ |
32
|
|
|
'error_code' => '404', |
33
|
|
|
'message' => 'Post not found!' |
34
|
|
|
], 404); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return response()->json(compact('post')); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function store(CreatePostRequest $request) |
41
|
|
|
{ |
42
|
|
|
$post = new Post(); |
43
|
|
|
$post->title = $request[ 'title' ]; |
|
|
|
|
44
|
|
|
$post->content = $request[ 'content' ]; |
|
|
|
|
45
|
|
|
$post->image = $request[ 'image' ]; |
|
|
|
|
46
|
|
|
$post->user_id = $request[ 'user_id' ]; |
|
|
|
|
47
|
|
|
|
48
|
|
|
return response()->json(compact('post')); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Update Post by ID |
53
|
|
|
* |
54
|
|
|
* @method update |
55
|
|
|
* |
56
|
|
|
* @param UpdatePostRequest $request |
57
|
|
|
* @param integer $id |
58
|
|
|
* |
59
|
|
|
* @return \Illuminate\Http\JsonResponse |
60
|
|
|
*/ |
61
|
|
|
|
62
|
|
|
public function update(UpdatePostRequest $request, $id) |
63
|
|
|
{ |
64
|
|
|
$post = Campaign::find($id); |
65
|
|
|
$post->title = $request[ 'title' ]; |
66
|
|
|
$post->content = $request[ 'content' ]; |
67
|
|
|
$post->image = $request[ 'image' ]; |
68
|
|
|
$post->user_id = $request[ 'user_id' ]; |
69
|
|
|
$post->save(); |
70
|
|
|
|
71
|
|
|
return response()->json([ 'message' => 'Post Updated' ]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function destroy($id) |
75
|
|
|
{ |
76
|
|
|
$post = Post::find($id); |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
if (!$post) { |
80
|
|
|
return response()->json( |
81
|
|
|
[ |
82
|
|
|
'error_code' => '404', |
83
|
|
|
'message' => 'Post not found!' |
84
|
|
|
], 404); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$post->delete(); |
88
|
|
|
|
89
|
|
|
return response()->json( |
90
|
|
|
[ |
91
|
|
|
'title' => $post->title, |
92
|
|
|
'content' => $post->content, |
93
|
|
|
'image' => $post->image, |
94
|
|
|
'user_id' => $post->user_id, |
95
|
|
|
'message' => 'Post delected', |
96
|
|
|
]); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.