MsgAnswersController.php
1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Message;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
class MsgAnswersController extends Controller
{
public function messages() {
$Msgs = Message::query()->orderByDesc('created_at')->paginate(25);
return view('admin.messages', compact('Msgs'));
}
public function admin_messages(Request $request) {
$id_admin = Auth::user()->id;
$users = User::query()->OrderBy('name')->get();
$Msgs = Message::query()->where('user_id', '=', $id_admin)
->orWhere('to_user_id', '=', $id_admin)
->orderByDesc('created_at')->paginate(5);
return view('admin.message.index', compact('Msgs', 'id_admin', 'users'));
}
public function admin_messages_post(Request $request) {
$rules = [
'title' => 'required|min:3|max:255',
'text' => 'required|min:1'
];
$messages = [
'required' => 'Поле не может быть пустым!',
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return redirect()->route('admin.admin-messages')->withErrors($validator);
} else {
$params = $request->all();
$id_admin = Auth::user()->id;
if ($request->has('file')) {
$params['file'] = $request->file('file')->store("upload/".$id_admin, 'public');
}
Message::create($params);
return redirect()->route('admin.admin-messages');
}
}
}