git add .
git commit "implement user management with search sort permissions and safe delete"
This commit is contained in:
@@ -3,9 +3,53 @@
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Admin\User\ChangePasswordRequest;
|
||||
use App\Http\Requests\Admin\User\UserUpdateRequest;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
//
|
||||
public function index()
|
||||
{
|
||||
$users = User::query()
|
||||
->applySearch()
|
||||
->applySort()
|
||||
->latest()
|
||||
->paginate(4)
|
||||
->withQueryString();
|
||||
|
||||
return view('admin.users.index',compact('users'));
|
||||
}
|
||||
|
||||
public function edit(User $user)
|
||||
{
|
||||
return view('admin.users.edit',compact('user'));
|
||||
}
|
||||
public function update(UserUpdateRequest $request,User $user)
|
||||
{
|
||||
$user->update($request->validated());
|
||||
return redirect()->route('admin.users.index');
|
||||
}
|
||||
|
||||
public function destroy(User $user)
|
||||
{
|
||||
$user->comments()->delete();
|
||||
$user->favorites()->delete();
|
||||
$user->reservations()->delete();
|
||||
$user->delete();
|
||||
|
||||
return redirect()->route('admin.users.index')
|
||||
->with('کاربر با موفقیت حذف شده.');
|
||||
}
|
||||
|
||||
public function ChangePassword(ChangePasswordRequest $request,User $user)
|
||||
{
|
||||
$user->update(['password'=>Hash::make($request->password)]);
|
||||
|
||||
return back();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin\User;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ChangePasswordRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'password' => [
|
||||
'required',
|
||||
'min:8',
|
||||
'confirmed',
|
||||
],
|
||||
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin\User;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UserUpdateRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'first_name'=>[
|
||||
'required',
|
||||
'string'
|
||||
],
|
||||
'last_name'=>[
|
||||
'required',
|
||||
'string'
|
||||
],
|
||||
'email'=>[
|
||||
'required',
|
||||
'email',
|
||||
Rule::unique('users','email')
|
||||
->ignore($this->user->id),
|
||||
],
|
||||
'mobile'=>[
|
||||
'required',
|
||||
|
||||
Rule::unique('users','mobile')
|
||||
->ignore($this->user->id),
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user