1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Auth; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use App\Thread; |
8
|
|
|
use App\ThreadTopic; |
9
|
|
|
|
10
|
|
|
class ThreadAskController extends Controller |
11
|
|
|
{ |
12
|
|
|
public function __construct() |
13
|
|
|
{ |
14
|
|
|
$this->middleware('auth', ['except' => [ |
15
|
|
|
'index', 'show' |
16
|
|
|
]]); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Display a listing of the resource. |
22
|
|
|
* |
23
|
|
|
* @return \Illuminate\Http\Response |
24
|
|
|
*/ |
25
|
|
|
public function index() |
26
|
|
|
{ |
27
|
|
|
$threads = Thread::orderBy('created_at', 'asc')->get(); |
28
|
|
|
return view('ask-index')->with('threads', $threads); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Show the form for creating a new resource. |
33
|
|
|
* |
34
|
|
|
* @return \Illuminate\Http\Response |
35
|
|
|
*/ |
36
|
|
|
public function create() |
37
|
|
|
{ |
38
|
|
|
$threads = Thread::orderBy('created_at', 'asc')->get(); |
39
|
|
|
return view('ask-form')->with('threads', $threads);; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Store a newly created resource in storage. |
44
|
|
|
* |
45
|
|
|
* @param Request $request |
46
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
47
|
|
|
* @throws \Illuminate\Validation\ValidationException |
48
|
|
|
*/ |
49
|
|
|
public function store(Request $request) |
50
|
|
|
{ |
51
|
|
|
$this->validate($request, [ |
52
|
|
|
'topic' => 'required|min:10', |
53
|
|
|
'question' => 'required|min:100' |
54
|
|
|
]); |
55
|
|
|
|
56
|
|
|
$topic = $this->addTopic($request->input('topic')); |
57
|
|
|
|
58
|
|
|
if($topic != null) { |
59
|
|
|
$thread = new Thread; |
60
|
|
|
$thread->user_id = $this->currentUser()->id; |
61
|
|
|
$thread->id_topic = $topic->id; |
62
|
|
|
$thread->question = $request->input('question'); |
63
|
|
|
|
64
|
|
|
if($thread->save()) { |
65
|
|
|
return redirect(route('user.thread.index'))->with('success', 'Pertanyaan dikirim !'); |
66
|
|
|
} |
67
|
|
|
return redirect()->back()->with('failed', 'Gagal mengirim pertanyaan, silahkan coba lagi nanti.'); |
68
|
|
|
} |
69
|
|
|
return redirect()->back()->with('failed', 'Gagal mengirim pertanyaan, silahkan coba lagi nanti.'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Display the specified resource. |
74
|
|
|
* |
75
|
|
|
* @param int $id |
76
|
|
|
* @return \Illuminate\Http\Response |
77
|
|
|
*/ |
78
|
|
|
public function show($id) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
// |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Show the form for editing the specified resource. |
85
|
|
|
* |
86
|
|
|
* @param int $id |
87
|
|
|
* @return \Illuminate\Http\Response |
88
|
|
|
*/ |
89
|
|
|
public function edit($id) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
// |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Update the specified resource in storage. |
96
|
|
|
* |
97
|
|
|
* @param \Illuminate\Http\Request $request |
98
|
|
|
* @param int $id |
99
|
|
|
* @return \Illuminate\Http\Response |
100
|
|
|
*/ |
101
|
|
|
public function update(Request $request, $id) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
// |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Remove the specified resource from storage. |
108
|
|
|
* |
109
|
|
|
* @param int $id |
110
|
|
|
* @return \Illuminate\Http\Response |
111
|
|
|
*/ |
112
|
|
|
public function destroy($id) |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
// |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
private function currentUser() { |
119
|
|
|
return Auth::guard('web')->user(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $topic |
124
|
|
|
* @return ThreadTopic|null |
125
|
|
|
*/ |
126
|
|
|
private function addTopic(string $topic) { |
127
|
|
|
$new = new ThreadTopic; |
128
|
|
|
$new->topic_name = $topic; |
129
|
|
|
if($new->save()) { |
130
|
|
|
return $new; |
131
|
|
|
} |
132
|
|
|
return null; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|