D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
forge
/
re-viewers.com
/
app
/
Http
/
Controllers
/
Admin
/
Filename :
UserController.php
back
Copy
<?php namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Storage; class UserController extends Controller { public function index(Request $request) { // Define the pagination limit $limit = 15; // Start with a query builder instance for the User model $users = User::query(); // Handle the search functionality if ($request->filled('keyword')) { $keyword = $request->keyword; $users->where(function ($query) use ($keyword) { $query->where('name', 'LIKE', '%' . $keyword . '%') ->orWhere('email', 'LIKE', '%' . $keyword . '%') ->orWhere('phone', 'LIKE', '%' . $keyword . '%'); }); } // Exclude user with ID 0 (your original code logic) $users->where('id', '<>', 0); // Paginate the results and sort them $users = $users->orderBy('id', 'desc')->paginate($limit); // This is a simple helper function to highlight search keywords, // which you can either add to a helper file or a trait. $highlightWords = function ($text, $word) { return preg_replace('#' . preg_quote($word, '#') . '#i', '<span style="background-color: #F9F902;">\\0</span>', $text); }; // Return the view with the users and keyword for highlighting return view('pages.user.index', [ 'users' => $users, 'keyword' => $request->keyword, 'highlightWords' => $highlightWords ]); } public function create() { $page_title = 'Add User'; return view('pages.user.form', compact('page_title')); } /** * Store a newly created user in the database. */ public function store(Request $request) { // 1. Validation $request->validate([ 'name' => 'required|string|max:255', 'email' => 'required|email|unique:users,email', 'password' => 'required|string|min:6', 'phone' => 'nullable|string|max:20', 'user_image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048', ]); // 2. Prepare data $data = $request->only('name', 'email', 'phone'); $data['user_type'] = 'Normal'; $data['password'] = Hash::make($request->password); // 3. Handle file upload if ($request->hasFile('user_image')) { $imageName = time() . '_' . $request->file('user_image')->getClientOriginalName(); $request->file('user_image')->storeAs('images', $imageName, 'public'); $data['user_image'] = $imageName; } // 4. Create user User::create($data); return redirect()->route('users.index')->with('success', 'User added successfully.'); } /** * Show the form for editing the specified user. */ public function edit(User $user) { $page_title = 'Edit User'; return view('pages.user.form', compact('user', 'page_title')); } /** * Update the specified user in the database. */ public function update(Request $request, User $user) { // 1. Validation $rules = [ 'name' => 'required|string|max:255', 'phone' => 'nullable|string|max:20', 'user_image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048', ]; // Add unique email validation if the user type is 'Normal' if ($user->user_type === 'Normal' || empty($user->user_type)) { $rules['email'] = 'required|email|unique:users,email,' . $user->id; $rules['password'] = 'nullable|string|min:6'; } $request->validate($rules); // 2. Prepare data $data = $request->only('name', 'email', 'phone'); // Only update the email if the user type is 'Normal' if ($user->user_type !== 'Normal' && $user->user_type !== null) { unset($data['email']); } if ($request->filled('password')) { $data['password'] = Hash::make($request->password); } // 3. Handle file upload if ($request->hasFile('user_image')) { // Delete old image if it exists if ($user->user_image) { Storage::disk('public')->delete('images/' . $user->user_image); } $imageName = time() . '_' . $request->file('user_image')->getClientOriginalName(); $request->file('user_image')->storeAs('images', $imageName, 'public'); $data['user_image'] = $imageName; } // 4. Update the user $user->update($data); return redirect()->route('users.index')->with('success', 'User updated successfully.'); } public function destroy(User $user) { if ($user->user_image) { Storage::disk('public')->delete('images/' . $user->user_image); } $user->delete(); return redirect()->route('users.index')->with('success', 'User updated successfully.'); } }