You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.8 KiB

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\User;
  4. use Illuminate\Http\Request;
  5. use Spatie\QueryBuilder\AllowedFilter;
  6. use Spatie\QueryBuilder\QueryBuilder;
  7. class UserController extends Controller
  8. {
  9. public function search(Request $request)
  10. {
  11. $limit = 20;
  12. $userQ = User::query();
  13. if ($request->filled('search')){
  14. $userQ = $userQ->where(function($query) use ($request) {
  15. $query->where('email', 'like', '%'.$request->search.'%')
  16. ->orWhere('username', 'like', '%'.$request->search.'%');
  17. });
  18. }
  19. return $userQ->where('id', '!=', auth()->id())
  20. ->whereNotIn('id', request('_business_info')['info']['users']->keys())
  21. ->select('id', 'name', 'email', 'username')->take($limit)->get();
  22. }
  23. public function index(Request $request)
  24. {
  25. $userQ = QueryBuilder::for(User::class)
  26. ->allowedFilters([
  27. AllowedFilter::exact('id'),
  28. ]);
  29. return $userQ->select('id', 'name', 'email', 'username')->get();
  30. }
  31. public function show($user)
  32. {
  33. return User::select('id', 'name', 'email', 'username')->findOrFail($user);
  34. }
  35. public function update(Request $request, string $user)
  36. {
  37. $user = User::findOrFail($user);
  38. $user->update($request->all());
  39. return $user;
  40. }
  41. public function setAvatar(Request $request, string $user)
  42. {
  43. $user = User::findOrFail($user);
  44. if ($request->hasFile('avatar')) {
  45. $user->saveAsAvatar($request->file('avatar'));
  46. }
  47. return $user;
  48. }
  49. public function unSetAvatar(Request $request, string $user)
  50. {
  51. $user = User::findOrFail($user);
  52. $user->deleteAvatar();
  53. return $user;
  54. }
  55. }