Adding user handling pages for admins.
This commit is contained in:
parent
c53a4155f4
commit
bd21a64191
9 changed files with 485 additions and 0 deletions
38
app/Http/Controllers/Admin/UserController.php
Normal file
38
app/Http/Controllers/Admin/UserController.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Http\Requests\Admin\UserRequest;
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
return view('admin.user.index');
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
return view('admin.user.form');
|
||||
}
|
||||
|
||||
public function edit(User $user)
|
||||
{
|
||||
return view('admin.user.form', [
|
||||
'model' => $user
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete user.
|
||||
*/
|
||||
public function destroy(User $user)
|
||||
{
|
||||
$user->delete();
|
||||
|
||||
return redirect()->route('admin.users.index')
|
||||
->with(['success' => "<strong>{$user->username}</strong> was deleted!"]);
|
||||
}
|
||||
}
|
||||
44
app/Http/Livewire/AdminUserTable.php
Normal file
44
app/Http/Livewire/AdminUserTable.php
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
use Livewire\Component;
|
||||
|
||||
class AdminUserTable extends Component
|
||||
{
|
||||
use Traits\WithPagination;
|
||||
|
||||
protected $queryString = [
|
||||
'username' => ['except' => ''],
|
||||
'role' => ['except' => '']
|
||||
];
|
||||
|
||||
/**
|
||||
* Filter by username
|
||||
*/
|
||||
public $username;
|
||||
|
||||
/**
|
||||
* Filter by role
|
||||
*/
|
||||
public $role;
|
||||
|
||||
public function render()
|
||||
{
|
||||
$query = User::query()->orderBy('username');
|
||||
|
||||
if (strlen($this->username) >= 3) {
|
||||
$query->where('username', 'LIKE', '%' . $this->username . '%');
|
||||
}
|
||||
|
||||
if (in_array($this->role, ['user','admin'])) {
|
||||
$query->where('role', $this->role);
|
||||
}
|
||||
|
||||
return view('livewire.admin-user-table', [
|
||||
'users' => $query->paginate($this->perPage)
|
||||
]);
|
||||
}
|
||||
}
|
||||
83
app/Http/Livewire/Form/Admin/UserForm.php
Normal file
83
app/Http/Livewire/Form/Admin/UserForm.php
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Form\Admin;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
use Livewire\Component;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class UserForm extends Component
|
||||
{
|
||||
use AuthorizesRequests;
|
||||
|
||||
/**
|
||||
* User object.
|
||||
*/
|
||||
public User $user;
|
||||
|
||||
/**
|
||||
* Password field
|
||||
*/
|
||||
public $password;
|
||||
|
||||
/**
|
||||
* Password confirmation field.
|
||||
*/
|
||||
public $password_confirmation;
|
||||
|
||||
/**
|
||||
* Initialize the component.
|
||||
*/
|
||||
public function mount(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'user.username' => ['required', 'min:4', Rule::unique('users', 'username')->ignore($this->user) ],
|
||||
'user.role' => 'required|in:user,admin',
|
||||
'password' => ($this->user->exists ? 'nullable' : 'required') . '|min:8|confirmed',
|
||||
];
|
||||
}
|
||||
|
||||
public function updated($property)
|
||||
{
|
||||
$this->validateOnly($property);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the user
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
$this->authorize('administrate');
|
||||
|
||||
$this->validate();
|
||||
|
||||
if ($this->password) {
|
||||
$this->user->password = Hash::make($this->password);
|
||||
}
|
||||
$this->user->save();
|
||||
|
||||
// Livewire redirect() does not have "with" method.
|
||||
// so we call session()->flash() directly instead.
|
||||
session()->flash('success', "<strong>{$this->user->username}</strong> was "
|
||||
. ($this->user->exists ? "updated!" : "created!"));
|
||||
return redirect()->route('admin.users.index');
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.form.admin.user');
|
||||
}
|
||||
}
|
||||
Reference in a new issue