1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Models\ForumTopic; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
|
8
|
|
|
class ForumTopicController extends Controller |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Display a listing of the resource. |
12
|
|
|
* |
13
|
|
|
* @return \Illuminate\Http\Response |
14
|
|
|
*/ |
15
|
|
|
public function index(Request $request) |
16
|
|
|
{ |
17
|
|
|
$query = ForumTopic::query(); |
18
|
|
|
|
19
|
|
|
if ($request->has('searchTerm')) { |
20
|
|
|
$columnsToSearch = ['title', 'email', 'phone']; |
21
|
|
|
$search_term = json_decode($request->searchTerm)->searchTerm; |
22
|
|
|
if (! empty($search_term)) { |
23
|
|
|
$searchQuery = '%'.$search_term.'%'; |
24
|
|
|
foreach ($columnsToSearch as $column) { |
25
|
|
|
$query->orWhere($column, 'LIKE', $searchQuery); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if ($request->has('columnFilters')) { |
31
|
|
|
$filters = get_object_vars(json_decode($request->columnFilters)); |
32
|
|
|
|
33
|
|
|
foreach ($filters as $key => $value) { |
34
|
|
|
if (! empty($value)) { |
35
|
|
|
$query->orWhere($key, 'like', '%'.$value.'%'); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($request->has('sort.0')) { |
41
|
|
|
$sort = json_decode($request->sort[0]); |
42
|
|
|
$query->orderBy($sort->field, $sort->type); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($request->has('perPage')) { |
46
|
|
|
$rows = $query->paginate($request->perPage); |
47
|
|
|
} else { |
48
|
|
|
$rows = $query->get(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $rows; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Show the form for creating a new resource. |
56
|
|
|
* |
57
|
|
|
* @return \Illuminate\Http\Response |
58
|
|
|
*/ |
59
|
|
|
public function create() |
60
|
|
|
{ |
61
|
|
|
// |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Store a newly created resource in storage. |
66
|
|
|
* |
67
|
|
|
* @param \Illuminate\Http\Request $request |
68
|
|
|
* @return \Illuminate\Http\Response |
69
|
|
|
*/ |
70
|
|
|
public function store(Request $request) |
71
|
|
|
{ |
72
|
|
|
$request->validate([ |
73
|
|
|
'category_id' => 'required|integer', |
74
|
|
|
'title' => 'required', |
75
|
|
|
'content' => 'required', |
76
|
|
|
]); |
77
|
|
|
|
78
|
|
|
return ForumTopic::create([ |
|
|
|
|
79
|
|
|
'category_id' => $request->category_id, |
80
|
|
|
'title' => $request->title, |
81
|
|
|
'content' => $request->content, |
82
|
|
|
'created_by' => $request->user()->id, |
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Display the specified resource. |
88
|
|
|
* |
89
|
|
|
* @param \App\Models\ForumTopic $forumTopic |
90
|
|
|
* @return \Illuminate\Http\Response |
91
|
|
|
*/ |
92
|
|
|
public function show($id) |
93
|
|
|
{ |
94
|
|
|
return ForumTopic::where('slug', $id)->with('category')->with('posts')->with('author')->first(); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Show the form for editing the specified resource. |
99
|
|
|
* |
100
|
|
|
* @param \App\Models\ForumTopic $forumTopic |
101
|
|
|
* @return \Illuminate\Http\Response |
102
|
|
|
*/ |
103
|
|
|
public function edit($id) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
// |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Update the specified resource in storage. |
110
|
|
|
* |
111
|
|
|
* @param \Illuminate\Http\Request $request |
112
|
|
|
* @param \App\Models\ForumTopic $forumTopic |
113
|
|
|
* @return \Illuminate\Http\Response |
114
|
|
|
*/ |
115
|
|
|
public function update(Request $request, $id) |
116
|
|
|
{ |
117
|
|
|
$request->validate([ |
118
|
|
|
'category_id' => 'required|integer', |
119
|
|
|
'title' => 'required', |
120
|
|
|
'content' => 'required', |
121
|
|
|
]); |
122
|
|
|
$topic = ForumTopic::find($id); |
123
|
|
|
if ($topic) { |
124
|
|
|
$topic->category_id = $request->category_id; |
125
|
|
|
$topic->title = $request->title; |
126
|
|
|
$topic->save(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Remove the specified resource from storage. |
132
|
|
|
* |
133
|
|
|
* @param \App\Models\ForumTopic $forumTopic |
134
|
|
|
* @return \Illuminate\Http\Response |
135
|
|
|
*/ |
136
|
|
|
public function destroy(ForumTopic $forumTopic) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
// |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|