UsersController.php 1.55 KB
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UsersController extends Controller
{
    public function index(Request $request) {
        $title = 'Админка - Пользователи системы';
        $id_admin = Auth::user()->id;
        if ($request->ajax()) {
            $user = User::find($request->id);
            $request->offsetUnset('id');
            $user->update($request->all());
        }
        $find_key = "";
        $users = User::query();
        if (isset($request->find)) {
           $find_key = $request->find;
           $users = $users->where('name', 'LIKE', "%$find_key%")
                        ->orWhere('email', 'LIKE', "%$find_key%");
        }
        $users = $users->paginate(15);

        if ($request->ajax()) {
           return view('admin.users.index_ajax', compact('users', 'id_admin'));
        } else {
            return view('admin.users.index', compact('users', 'title', 'id_admin', 'find_key'));
        }
    }

    public function roles(Request $request) {
        if ($request->ajax()) {
            $user = User::find($request->id);
            $request->offsetUnset('id');
            $user->update($request->all());
        }

        $users = User::query()->paginate(15);

        if ($request->ajax()) {
            return view('admin.users.roles.index_ajax', compact('users'));
        } else {
            return view('admin.users.roles.index', compact('users'));
        }
    }
}