Blame view
app/Http/Controllers/Admin/MsgAnswersController.php
3.2 KB
82a9544dc Связи моделей, гр... |
1 2 3 4 5 6 |
<?php namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Models\Message; |
e688e0d8a Статистика работн... |
7 |
use App\Models\User; |
82a9544dc Связи моделей, гр... |
8 |
use Illuminate\Http\Request; |
e688e0d8a Статистика работн... |
9 |
use Illuminate\Support\Facades\Auth; |
93a3f79f0 Поисковый движок ... |
10 |
use Illuminate\Support\Facades\DB; |
e688e0d8a Статистика работн... |
11 |
use Illuminate\Support\Facades\Validator; |
82a9544dc Связи моделей, гр... |
12 13 14 15 |
class MsgAnswersController extends Controller { public function messages() { |
00652ea57 Оптимизация запро... |
16 |
$Msgs = Message::with('user_from')->with('user_to')->with('response')->orderByDesc('created_at')->paginate(25); |
82a9544dc Связи моделей, гр... |
17 18 19 |
return view('admin.messages', compact('Msgs')); } |
e688e0d8a Статистика работн... |
20 21 |
public function admin_messages(Request $request) { |
f060aa75b Счетчик сообщений... |
22 23 24 25 26 |
if ($request->ajax()) { $msg = Message::find($request->id); $msg->flag_new = !($request->flag_new); $msg->save(); } |
e688e0d8a Статистика работн... |
27 28 |
$id_admin = Auth::user()->id; $users = User::query()->OrderBy('name')->get(); |
00652ea57 Оптимизация запро... |
29 30 |
$Msgs = Message::with('user_from')->with('user_to')->with('response') ->where('user_id', '=', $id_admin) |
e688e0d8a Статистика работн... |
31 32 |
->orWhere('to_user_id', '=', $id_admin) ->orderByDesc('created_at')->paginate(5); |
f060aa75b Счетчик сообщений... |
33 34 35 36 |
if ($request->ajax()) return view('admin.message.index_ajax', compact('Msgs', 'id_admin', 'users')); else return view('admin.message.index', compact('Msgs', 'id_admin', 'users')); |
e688e0d8a Статистика работн... |
37 |
} |
93a3f79f0 Поисковый движок ... |
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
public function messages_sql(Request $request) { $id = Auth::user()->id; DB::enableQueryLog(); //$query = DB::select('select * from users where id = :id', ['id' => 1]); $query = DB::select(DB::raw('SELECT u1.name as "To-user", u2.name as "From-user", m1.`text`, m1.created_at FROM messages m1 JOIN (SELECT MAX(id) id FROM messages GROUP BY LEAST(user_id, to_user_id), GREATEST(user_id, to_user_id) ) m2 USING (id) JOIN users u1 ON u1.id = m1.user_id JOIN users u2 ON u2.id = m1.to_user_id Where ((m1.user_id = :uid) or (m1.to_user_id = :uid2)) '), ['uid' => $id, 'uid2' => $id]); //dump(DB::getQueryLog()); dd($query); return; } |
e688e0d8a Статистика работн... |
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
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'); } } |
82a9544dc Связи моделей, гр... |
80 |
} |