Commit 1a8d1192ea35c52d10b1e40c3aeb6bbc4bdd1339

Authored by Андрей Ларионов
1 parent 0ea9852237

Коммит в админке - баг просмотра сообщений

Showing 2 changed files with 4 additions and 4 deletions Inline Diff

app/Http/Controllers/Admin/MsgAnswersController.php
1 <?php 1 <?php
2 2
3 namespace App\Http\Controllers\Admin; 3 namespace App\Http\Controllers\Admin;
4 4
5 use App\Http\Controllers\Controller; 5 use App\Http\Controllers\Controller;
6 use App\Models\Message; 6 use App\Models\Message;
7 use App\Models\User; 7 use App\Models\User;
8 use Illuminate\Database\Eloquent\Builder; 8 use Illuminate\Database\Eloquent\Builder;
9 use Illuminate\Http\Request; 9 use Illuminate\Http\Request;
10 use Illuminate\Support\Facades\Auth; 10 use Illuminate\Support\Facades\Auth;
11 use Illuminate\Support\Facades\DB; 11 use Illuminate\Support\Facades\DB;
12 use Illuminate\Support\Facades\Validator; 12 use Illuminate\Support\Facades\Validator;
13 13
14 class MsgAnswersController extends Controller 14 class MsgAnswersController extends Controller
15 { 15 {
16 public function messages(Request $request) { 16 public function messages(Request $request) {
17 $find_key = ""; 17 $find_key = "";
18 $find_cat = ""; 18 $find_cat = "";
19 $Msgs = Message::with('user_from')->with('user_to')->with('response'); 19 $Msgs = Message::with('user_from')->with('user_to'); //->with('response');
20 20
21 $Msgs = $this->filter($Msgs, $request, $find_key, $find_cat); 21 $Msgs = $this->filter($Msgs, $request, $find_key, $find_cat);
22 22
23 $Msgs = $Msgs->orderByDesc('created_at')->paginate(25); 23 $Msgs = $Msgs->orderByDesc('created_at')->paginate(25);
24 24
25 return view('admin.messages', compact('Msgs', 'find_key', 'find_cat')); 25 return view('admin.messages', compact('Msgs', 'find_key', 'find_cat'));
26 } 26 }
27 27
28 public function read_message(Message $message) { 28 public function read_message(Message $message) {
29 return view('admin.message-read', compact('message')); 29 return view('admin.message-read', compact('message'));
30 } 30 }
31 31
32 public function filter($Msgs, Request $request, &$find_key, &$find_cat) { 32 public function filter($Msgs, Request $request, &$find_key, &$find_cat) {
33 if (isset($request->find)) { 33 if (isset($request->find)) {
34 $find_key = $request->find; 34 $find_key = $request->find;
35 $Msgs = $Msgs->where(function($q) use ($find_key) { 35 $Msgs = $Msgs->where(function($q) use ($find_key) {
36 $q->whereHas('user_from', 36 $q->whereHas('user_from',
37 function (Builder $query) use ($find_key) { 37 function (Builder $query) use ($find_key) {
38 $query->where('name', 'LIKE', "%$find_key%"); 38 $query->where('name', 'LIKE', "%$find_key%");
39 } 39 }
40 )-> 40 )->
41 orWhereHas('user_to', 41 orWhereHas('user_to',
42 function (Builder $query) use ($find_key) { 42 function (Builder $query) use ($find_key) {
43 $query->where('name', 'LIKE', "%$find_key%"); 43 $query->where('name', 'LIKE', "%$find_key%");
44 } 44 }
45 ); 45 );
46 }); 46 });
47 } 47 }
48 48
49 if (isset($request->category)) { 49 if (isset($request->category)) {
50 $find_cat = $request->category; 50 $find_cat = $request->category;
51 51
52 switch ($find_cat) { 52 switch ($find_cat) {
53 case 'Работодатели': 53 case 'Работодатели':
54 $Msgs = $Msgs->where(function($q) { 54 $Msgs = $Msgs->where(function($q) {
55 $q->whereHas('user_to', 55 $q->whereHas('user_to',
56 function (Builder $query) { 56 function (Builder $query) {
57 $query->where('is_worker', '0'); 57 $query->where('is_worker', '0');
58 } 58 }
59 )->orwhereHas('user_from', 59 )->orwhereHas('user_from',
60 function (Builder $query) { 60 function (Builder $query) {
61 $query->where('is_worker', '0'); 61 $query->where('is_worker', '0');
62 } 62 }
63 ); 63 );
64 }); 64 });
65 break; 65 break;
66 case 'Соискатели': 66 case 'Соискатели':
67 $Msgs = $Msgs->where(function($q) { 67 $Msgs = $Msgs->where(function($q) {
68 $q->whereHas('user_to', 68 $q->whereHas('user_to',
69 function (Builder $query) { 69 function (Builder $query) {
70 $query->where('is_worker', '1'); 70 $query->where('is_worker', '1');
71 } 71 }
72 )->orwhereHas('user_from', 72 )->orwhereHas('user_from',
73 function (Builder $query) { 73 function (Builder $query) {
74 $query->where('is_worker', '1'); 74 $query->where('is_worker', '1');
75 } 75 }
76 ); 76 );
77 }); 77 });
78 break; 78 break;
79 case 'Администраторы': 79 case 'Администраторы':
80 $Msgs = $Msgs->where(function($q) { 80 $Msgs = $Msgs->where(function($q) {
81 $q->whereHas('user_to', 81 $q->whereHas('user_to',
82 function (Builder $query) { 82 function (Builder $query) {
83 $query->where('admin', '1'); 83 $query->where('admin', '1');
84 } 84 }
85 )->orwhereHas('user_from', 85 )->orwhereHas('user_from',
86 function (Builder $query) { 86 function (Builder $query) {
87 $query->where('admin', '1'); 87 $query->where('admin', '1');
88 } 88 }
89 ); 89 );
90 }); 90 });
91 break; 91 break;
92 default:break; 92 default:break;
93 } 93 }
94 } 94 }
95 95
96 return $Msgs; 96 return $Msgs;
97 97
98 } 98 }
99 99
100 public function admin_messages(Request $request) { 100 public function admin_messages(Request $request) {
101 if ($request->ajax()) { 101 if ($request->ajax()) {
102 $msg = Message::find($request->id); 102 $msg = Message::find($request->id);
103 $msg->flag_new = !($request->flag_new); 103 $msg->flag_new = !($request->flag_new);
104 $msg->save(); 104 $msg->save();
105 } 105 }
106 106
107 $id_admin = Auth::user()->id; 107 $id_admin = Auth::user()->id;
108 $users = User::query()->OrderBy('name')->where('is_bd', '=', '0')->get(); 108 $users = User::query()->OrderBy('name')->where('is_bd', '=', '0')->get();
109 109
110 $Msgs = Message::with('user_from')->with('user_to')->with('response') 110 $Msgs = Message::with('user_from')->with('user_to')->with('response')
111 ->where(function($query) use ($id_admin) { 111 ->where(function($query) use ($id_admin) {
112 $query->where('user_id', '=', $id_admin) 112 $query->where('user_id', '=', $id_admin)
113 ->orWhere('to_user_id', '=', $id_admin); 113 ->orWhere('to_user_id', '=', $id_admin);
114 }); 114 });
115 115
116 $find_key = ''; 116 $find_key = '';
117 $find_cat = ''; 117 $find_cat = '';
118 118
119 $Msgs = $this->filter($Msgs, $request, $find_key, $find_cat); 119 $Msgs = $this->filter($Msgs, $request, $find_key, $find_cat);
120 120
121 $Msgs = $Msgs->orderByDesc('created_at')->paginate(5); 121 $Msgs = $Msgs->orderByDesc('created_at')->paginate(5);
122 122
123 if ($request->ajax()) 123 if ($request->ajax())
124 return view('admin.message.index_ajax', compact('Msgs', 'id_admin', 'users')); 124 return view('admin.message.index_ajax', compact('Msgs', 'id_admin', 'users'));
125 else 125 else
126 return view('admin.message.index', compact('Msgs', 'id_admin', 'users', 'find_key', 'find_cat')); 126 return view('admin.message.index', compact('Msgs', 'id_admin', 'users', 'find_key', 'find_cat'));
127 } 127 }
128 128
129 public function messages_sql(Request $request) { 129 public function messages_sql(Request $request) {
130 $id = Auth::user()->id; 130 $id = Auth::user()->id;
131 DB::enableQueryLog(); 131 DB::enableQueryLog();
132 //$query = DB::select('select * from users where id = :id', ['id' => 1]); 132 //$query = DB::select('select * from users where id = :id', ['id' => 1]);
133 $query = DB::select(DB::raw('SELECT u1.name as "To-user", u2.name as "From-user", m1.`text`, m1.created_at 133 $query = DB::select(DB::raw('SELECT u1.name as "To-user", u2.name as "From-user", m1.`text`, m1.created_at
134 FROM messages m1 134 FROM messages m1
135 JOIN (SELECT MAX(id) id FROM messages 135 JOIN (SELECT MAX(id) id FROM messages
136 GROUP BY LEAST(user_id, to_user_id), 136 GROUP BY LEAST(user_id, to_user_id),
137 GREATEST(user_id, to_user_id) 137 GREATEST(user_id, to_user_id)
138 ) m2 USING (id) 138 ) m2 USING (id)
139 JOIN users u1 ON u1.id = m1.user_id 139 JOIN users u1 ON u1.id = m1.user_id
140 JOIN users u2 ON u2.id = m1.to_user_id 140 JOIN users u2 ON u2.id = m1.to_user_id
141 Where ((m1.user_id = :uid) or (m1.to_user_id = :uid2)) 141 Where ((m1.user_id = :uid) or (m1.to_user_id = :uid2))
142 '), ['uid' => $id, 'uid2' => $id]); 142 '), ['uid' => $id, 'uid2' => $id]);
143 //dump(DB::getQueryLog()); 143 //dump(DB::getQueryLog());
144 dd($query); 144 dd($query);
145 return; 145 return;
146 } 146 }
147 147
148 public function admin_messages_post(Request $request) { 148 public function admin_messages_post(Request $request) {
149 $rules = [ 149 $rules = [
150 'title' => 'required|min:3|max:255', 150 'title' => 'required|min:3|max:255',
151 'text' => 'required|min:1' 151 'text' => 'required|min:1'
152 ]; 152 ];
153 153
154 $messages = [ 154 $messages = [
155 'required' => 'Поле не может быть пустым!', 155 'required' => 'Поле не может быть пустым!',
156 ]; 156 ];
157 157
158 $validator = Validator::make($request->all(), $rules, $messages); 158 $validator = Validator::make($request->all(), $rules, $messages);
159 159
160 if ($validator->fails()) { 160 if ($validator->fails()) {
161 return redirect()->route('admin.admin-messages')->withErrors($validator); 161 return redirect()->route('admin.admin-messages')->withErrors($validator);
162 } else { 162 } else {
163 $params = $request->all(); 163 $params = $request->all();
164 $id_admin = Auth::user()->id; 164 $id_admin = Auth::user()->id;
165 if ($request->has('file')) { 165 if ($request->has('file')) {
166 $params['file'] = $request->file('file')->store("upload/".$id_admin, 'public'); 166 $params['file'] = $request->file('file')->store("upload/".$id_admin, 'public');
167 } 167 }
168 Message::create($params); 168 Message::create($params);
169 return redirect()->route('admin.admin-messages'); 169 return redirect()->route('admin.admin-messages');
170 } 170 }
171 } 171 }
172 } 172 }
173 173
resources/views/admin/messages.blade.php
1 @extends('layout.admin', ['title' => 'Админка - Сообщения']) 1 @extends('layout.admin', ['title' => 'Админка - Сообщения'])
2 2
3 @section('script') 3 @section('script')
4 @endsection 4 @endsection
5 5
6 @section('search') 6 @section('search')
7 @include('admin.find_message') 7 @include('admin.find_message')
8 @endsection 8 @endsection
9 9
10 @section('content') 10 @section('content')
11 <div class="w-full overflow-hidden rounded-lg shadow-xs" id="ajax_block"> 11 <div class="w-full overflow-hidden rounded-lg shadow-xs" id="ajax_block">
12 <div class="w-full overflow-x-auto"> 12 <div class="w-full overflow-x-auto">
13 <table class="w-full whitespace-no-wrap"> 13 <table class="w-full whitespace-no-wrap">
14 <thead> 14 <thead>
15 <tr 15 <tr
16 class="text-xs font-semibold tracking-wide text-left text-gray-500 uppercase border-b dark:border-gray-700 bg-gray-50 dark:text-gray-400 dark:bg-gray-800" 16 class="text-xs font-semibold tracking-wide text-left text-gray-500 uppercase border-b dark:border-gray-700 bg-gray-50 dark:text-gray-400 dark:bg-gray-800"
17 > 17 >
18 <th class="px-4 py-3">№</th> 18 <th class="px-4 py-3">№</th>
19 <th class="px-4 py-3">От юзера</th> 19 <th class="px-4 py-3">От юзера</th>
20 <th class="px-4 py-3">К юзеру</th> 20 <th class="px-4 py-3">К юзеру</th>
21 <th class="px-4 py-3">Отклик</th> 21 <th class="px-4 py-3" style="display:none">Отклик</th>
22 <th class="px-4 py-3">Читать</th> 22 <th class="px-4 py-3">Читать</th>
23 <th class="px-4 py-3">Дата</th> 23 <th class="px-4 py-3">Дата</th>
24 </tr> 24 </tr>
25 </thead> 25 </thead>
26 <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800"> 26 <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
27 @foreach($Msgs as $msg) 27 @foreach($Msgs as $msg)
28 <tr class="text-gray-700 dark:text-gray-400"> 28 <tr class="text-gray-700 dark:text-gray-400">
29 <td class="px-4 py-3"> 29 <td class="px-4 py-3">
30 {{$msg->id}} 30 {{$msg->id}}
31 </td> 31 </td>
32 <td class="px-4 py-3"> 32 <td class="px-4 py-3">
33 @if (isset($msg->user_from->id)) 33 @if (isset($msg->user_from->id))
34 {{$msg->user_from->name}} ({{$msg->user_from->id}}) 34 {{$msg->user_from->name}} ({{$msg->user_from->id}})
35 @else 35 @else
36 Пользователь удален 36 Пользователь удален
37 @endif 37 @endif
38 </td> 38 </td>
39 <td class="px-4 py-3"> 39 <td class="px-4 py-3">
40 @if (isset($msg->user_to->id)) 40 @if (isset($msg->user_to->id))
41 {{$msg->user_to->name}} ({{$msg->user_to->id}}) 41 {{$msg->user_to->name}} ({{$msg->user_to->id}})
42 @else 42 @else
43 Пользователь удален 43 Пользователь удален
44 @endif 44 @endif
45 </td> 45 </td>
46 <td class="px-4 py-3"> 46 <td class="px-4 py-3" style="display:none">
47 <div class="flex items-center text-sm"> 47 <div class="flex items-center text-sm">
48 <div> 48 <div>
49 @if ($msg->response->count()) 49 @if (true) //$msg->response->count())
50 Да 50 Да
51 @else 51 @else
52 Нет 52 Нет
53 @endif 53 @endif
54 </div> 54 </div>
55 </div> 55 </div>
56 </td> 56 </td>
57 <td class="px-4 py-3 text-sm"> 57 <td class="px-4 py-3 text-sm">
58 <a style="text-decoration: underline;" href="{{ route('admin.read-message', ['message' => $msg->id]) }}">Читать</a> 58 <a style="text-decoration: underline;" href="{{ route('admin.read-message', ['message' => $msg->id]) }}">Читать</a>
59 </td> 59 </td>
60 <td class="px-4 py-3 text-sm"> 60 <td class="px-4 py-3 text-sm">
61 {{ $msg->created_at }} 61 {{ $msg->created_at }}
62 </td> 62 </td>
63 </tr> 63 </tr>
64 @endforeach 64 @endforeach
65 </tbody> 65 </tbody>
66 </table> 66 </table>
67 </div> 67 </div>
68 68
69 <div class="grid px-4 py-3 text-xs font-semibold tracking-wide text-gray-500 uppercase border-t dark:border-gray-700 bg-gray-50 sm:grid-cols-9 dark:text-gray-400 dark:bg-gray-800"> 69 <div class="grid px-4 py-3 text-xs font-semibold tracking-wide text-gray-500 uppercase border-t dark:border-gray-700 bg-gray-50 sm:grid-cols-9 dark:text-gray-400 dark:bg-gray-800">
70 <?=$Msgs->appends($_GET)->links('admin.pagginate'); ?> 70 <?=$Msgs->appends($_GET)->links('admin.pagginate'); ?>
71 </div> 71 </div>
72 </div> 72 </div>
73 @endsection 73 @endsection
74 74