diff --git a/app/Http/Controllers/Admin/MsgAnswersController.php b/app/Http/Controllers/Admin/MsgAnswersController.php index 18350ad..d4d8218 100644 --- a/app/Http/Controllers/Admin/MsgAnswersController.php +++ b/app/Http/Controllers/Admin/MsgAnswersController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Models\Message; use App\Models\User; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; @@ -12,10 +13,88 @@ use Illuminate\Support\Facades\Validator; class MsgAnswersController extends Controller { - public function messages() { - $Msgs = Message::with('user_from')->with('user_to')->with('response')->orderByDesc('created_at')->paginate(25); + public function messages(Request $request) { + $find_key = ""; + $find_cat = ""; + $Msgs = Message::with('user_from')->with('user_to')->with('response'); + + $Msgs = $this->filter($Msgs, $request, $find_key, $find_cat); + + $Msgs = $Msgs->orderByDesc('created_at')->paginate(25); + + return view('admin.messages', compact('Msgs', 'find_key', 'find_cat')); + } + + public function read_message(Message $message) { + return view('admin.message-read', compact('message')); + } + + public function filter($Msgs, Request $request, &$find_key, &$find_cat) { + if (isset($request->find)) { + $find_key = $request->find; + $Msgs = $Msgs->where(function($q) use ($find_key) { + $q->whereHas('user_from', + function (Builder $query) use ($find_key) { + $query->where('name', 'LIKE', "%$find_key%"); + } + )-> + orWhereHas('user_to', + function (Builder $query) use ($find_key) { + $query->where('name', 'LIKE', "%$find_key%"); + } + ); + }); + } + + if (isset($request->category)) { + $find_cat = $request->category; + + switch ($find_cat) { + case 'Работодатели': + $Msgs = $Msgs->where(function($q) { + $q->whereHas('user_to', + function (Builder $query) { + $query->where('is_worker', '0'); + } + )->orwhereHas('user_from', + function (Builder $query) { + $query->where('is_worker', '0'); + } + ); + }); + break; + case 'Соискатели': + $Msgs = $Msgs->where(function($q) { + $q->whereHas('user_to', + function (Builder $query) { + $query->where('is_worker', '1'); + } + )->orwhereHas('user_from', + function (Builder $query) { + $query->where('is_worker', '1'); + } + ); + }); + break; + case 'Администраторы': + $Msgs = $Msgs->where(function($q) { + $q->whereHas('user_to', + function (Builder $query) { + $query->where('admin', '1'); + } + )->orwhereHas('user_from', + function (Builder $query) { + $query->where('admin', '1'); + } + ); + }); + break; + default:break; + } + } + + return $Msgs; - return view('admin.messages', compact('Msgs')); } public function admin_messages(Request $request) { @@ -29,14 +108,22 @@ class MsgAnswersController extends Controller $users = User::query()->OrderBy('name')->get(); $Msgs = Message::with('user_from')->with('user_to')->with('response') - ->where('user_id', '=', $id_admin) - ->orWhere('to_user_id', '=', $id_admin) - ->orderByDesc('created_at')->paginate(5); + ->where(function($query) use ($id_admin) { + $query->where('user_id', '=', $id_admin) + ->orWhere('to_user_id', '=', $id_admin); + }); + + $find_key = ''; + $find_cat = ''; + + $Msgs = $this->filter($Msgs, $request, $find_key, $find_cat); + + $Msgs = $Msgs->orderByDesc('created_at')->paginate(5); 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')); + return view('admin.message.index', compact('Msgs', 'id_admin', 'users', 'find_key', 'find_cat')); } public function messages_sql(Request $request) { diff --git a/resources/views/admin/find_message.blade.php b/resources/views/admin/find_message.blade.php new file mode 100644 index 0000000..eaca810 --- /dev/null +++ b/resources/views/admin/find_message.blade.php @@ -0,0 +1,41 @@ +