Procházet zdrojové kódy

added phone to users, refactored users table

Alexander Musikhin před 8 měsíci
rodič
revize
50e4d4689e

+ 29 - 2
app/Http/Controllers/UserController.php

@@ -14,15 +14,42 @@ class UserController extends Controller
     protected array $data = [
         'active' => 'users',
         'title'  => 'Пользователи',
+        'id'        => 'users',
+        'header'    => [
+            'id'            => 'ID',
+            'email'         => 'E-mail',
+            'name'          => 'ФИО',
+            'phone'         => 'Телефон',
+            'role'          => 'Роль',
+            'created_at'    => 'Дата создания'
+        ],
+        'searchFields' => [
+            'name',
+            'phone',
+            'email',
+        ],
+        'ranges' => [],
+        'filters' => [],
     ];
 
 
     /**
      * Display a listing of the resource.
      */
-    public function index()
+    public function index(Request $request)
     {
-        $this->data['users'] = User::query()->get();
+        $model = new User;
+        $this->createFilters($model, 'role');
+        $this->createDateFilters($model, 'created_at');
+
+        $q = $model::query();
+        $this->acceptFilters($q, $request);
+        $this->acceptSearch($q, $request);
+        $this->setSortAndOrderBy($model, $request);
+
+        $q->orderBy($this->data['sortBy'], $this->data['orderBy']);
+        $this->data['users'] = $q->paginate()->withQueryString();
+
         return view('users.index', $this->data);
     }
 

+ 1 - 0
app/Http/Requests/User/StoreProfile.php

@@ -23,6 +23,7 @@ class StoreProfile extends FormRequest
     {
         return [
             'name'              => 'string|required|min:2',
+            'phone'             => 'nullable|string',
             'current_password'  => 'nullable',
             'password'          => 'nullable|min:8|confirmed',
         ];

+ 2 - 1
app/Http/Requests/User/StoreUser.php

@@ -18,7 +18,7 @@ class StoreUser extends FormRequest
     /**
      * Get the validation rules that apply to the request.
      *
-     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
+     * @return array
      */
     public function rules(): array
     {
@@ -26,6 +26,7 @@ class StoreUser extends FormRequest
             'id'        => 'nullable|exists:users',
             'email'     => 'required_if:id,null|email|unique:users,email,'.$this->id,
             'name'      => 'required|string|min:2',
+            'phone'     => 'nullable|string',
             'password'  => 'required_without:id|nullable|string|min:4',
             'role'      => 'nullable|string|in:' . implode(',', Role::VALID_ROLES),
         ];

+ 3 - 0
app/Models/User.php

@@ -10,6 +10,8 @@ class User extends Authenticatable implements MustVerifyEmail
 {
     use Notifiable;
 
+    const DEFAULT_SORT_BY = 'created_at';
+
     /**
      * The attributes that are mass assignable.
      *
@@ -18,6 +20,7 @@ class User extends Authenticatable implements MustVerifyEmail
     protected $fillable = [
         'name',
         'email',
+        'phone',
         'password',
         'role',
     ];

+ 1 - 0
database/migrations/0001_01_01_000000_create_users_table.php

@@ -15,6 +15,7 @@ return new class extends Migration
             $table->id();
             $table->string('name');
             $table->string('email')->unique();
+            $table->string('phone')->nullable();
             $table->timestamp('email_verified_at')->nullable();
             $table->string('password');
             $table->string('role');

+ 1 - 0
resources/views/users/edit.blade.php

@@ -18,6 +18,7 @@
                                             'disabled' => ($user && $user->email_verified_at),
                                             ])
                 @include('partials.input', ['name' => 'name', 'title' => 'Имя', 'required' => true, 'value' => $user->name ?? ''])
+                @include('partials.input', ['name' => 'phone', 'title' => 'Телефон', 'value' => $user->phone ?? ''])
 
 {{--                @include('partials.avatars', ['user' => $user])--}}
                 @include('partials.input', ['name' => 'password', 'type' => 'password', 'title' => 'Пароль'])

+ 48 - 31
resources/views/users/index.blade.php

@@ -1,38 +1,55 @@
 @extends('layouts.app')
 
 @section('content')
-    <a href="{{ route('user.create') }}" class="btn btn-sm btn-primary">Создать</a>
-    <div class="px-md-3 px-2">
-        <div class="table">
-            <div class="row d-none d-md-flex header px-5 py-4 fw-bold">
-                <div class="col-xl-3">Имя пользователя</div>
-                <div class="col-xl-3">Email</div>
-                <div class="col-xl-3">Роль</div>
-                <div class="col-xl-3 text-end">Действия</div>
-            </div>
-            @foreach($users as $user)
-                <div class="row px-md-5 px-2 py-4 align-items-center">
-                    <div class="col-xl-3 mb-3">
-                        <span class="ps-2">{{ $user->name }}</span>
-                    </div>
-                    <div class="col-xl-3">
-                        <span class="d-md-none fw-bold">E-mail: </span>{{ $user->email }}
-                    </div>
-                    <div class="col-xl-3">
-                        <span class="d-md-none fw-bold">Роль: </span>{{ roleName($user->role) }}
-                    </div>
-                    <div class="col-xl-3 text-end">
-                        <a href="{{ route('user.show', $user->id) }}"><i class="bi bi-pencil fs-5 text-primary"></i></a>
-                        <a href="#" onclick="if(confirm('Удалить пользователя {{ $user->name }}?')) { document.getElementById('delete-user-{{ $user->id }}').submit(); } ">
-                            <i class="bi bi-trash fs-5 text-danger"></i>
-                        </a>
-                        <form action="{{ route('user.destroy', $user->id) }}" method="post" class="visually-hidden d-none" id="delete-user-{{ $user->id }}">
-                            @csrf
-                            @method('DELETE')
-                        </form>
-                    </div>
+    <div class="row mb-3">
+        <div class="col-6">
+            <h3>Пользователи</h3>
+        </div>
+        <div class="col-6 text-end">
+            <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addModal">
+                Добавить
+            </button>
+
+        </div>
+    </div>
+
+
+    @include('partials.table', [
+        'id'        => $id,
+        'header'    => $header,
+        'strings'   => $users,
+        'routeName' => 'user.show',
+    ])
+
+    <div class="row pt-3 px-3">
+        <div class="col-12 pagination">
+            {{ $users->links() }}
+        </div>
+    </div>
+
+    <!-- Модальное окно добавления-->
+    <div class="modal fade" id="addModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
+        <div class="modal-dialog modal-fullscreen-sm-down modal-lg">
+            <div class="modal-content">
+                <div class="modal-header">
+                    <h1 class="modal-title fs-5" id="addModalLabel">Добавить заказ МАФ</h1>
+                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
+                </div>
+                <div class="modal-body">
+                    <form action="{{ route('user.store') }}" method="post">
+                        @csrf
+
+                        @include('partials.input', ['name' => 'email', 'type' => 'email', 'title' => 'Email', 'required' => true])
+                        @include('partials.input', ['name' => 'name', 'title' => 'Имя', 'required' => true])
+                        @include('partials.input', ['name' => 'phone', 'title' => 'Телефон'])
+                        @include('partials.input', ['name' => 'password', 'type' => 'password', 'title' => 'Пароль', 'required' => true])
+                        @include('partials.select', ['name' => 'role', 'title' => 'Роль', 'options' => getRoles(), 'value' => $user->role ?? \App\Models\Role::MANAGER])
+
+                        @include('partials.submit', ['name' => 'Добавить'])
+
+                    </form>
                 </div>
-            @endforeach
+            </div>
         </div>
     </div>