|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Validator; |
|
6
|
|
|
use App\Contact; |
|
7
|
|
|
use App\Http\Requests; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
|
|
10
|
|
|
class ContactController extends Controller |
|
11
|
|
|
{ |
|
12
|
|
|
public function index(Request $request) |
|
13
|
|
|
{ |
|
14
|
|
|
if ($request->wantsJson()) { |
|
15
|
|
|
return Contact::all(); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
return view('template'); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function store(Request $request) |
|
22
|
|
|
{ |
|
23
|
|
|
if (!$request->wantsJson()) { |
|
24
|
|
|
return response("Contacts can only be created via json requests", 400); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$requestData = $request->all(); |
|
28
|
|
|
|
|
29
|
|
|
$validator = Validator::make($requestData, [ |
|
30
|
|
|
'name' => 'required|max:255', |
|
31
|
|
|
'email' => 'required|email|max:255|unique:contacts,email,NULL,id,deleted_at,NULL', |
|
32
|
|
|
'filter_tags' => 'max:255', |
|
33
|
|
|
]); |
|
34
|
|
|
|
|
35
|
|
|
if ($validator->fails()) { |
|
36
|
|
|
return response($validator->getMessageBag(), 422); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$contact = new Contact; |
|
40
|
|
|
$contact->name = $requestData['name']; |
|
41
|
|
|
$contact->email = $requestData['email']; |
|
42
|
|
|
$contact->filter_tags = $requestData['filter_tags']; |
|
43
|
|
|
$contact->active = true; |
|
44
|
|
|
$contact->save(); |
|
45
|
|
|
|
|
46
|
|
|
//Re-fetch so we have a full record |
|
47
|
|
|
return Contact::findOrFail($contact->id); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function destroy(Request $request, $id) |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
Contact::findOrFail($id)->delete(); |
|
53
|
|
|
|
|
54
|
|
|
return response(null, 204); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read 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.