Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Wyszukiwarka użytkowników w Laravel
northwest
post 12.06.2019, 09:54:38
Post #1





Grupa: Zarejestrowani
Postów: 788
Pomógł: 1
Dołączył: 17.09.2004

Ostrzeżenie: (10%)
X----


Witam serdecznie.
Zaczynam przygodę z Laravelem i natrafiłem na mały problem sad.gif
Wykorzystuję w moim projekcie Laravel 5.8.

Mam działający kod wyświetlający listę użytkowników.

Mój kod wygląda następująco:


Users:
  1. class User extends Authenticatable implements MustVerifyEmail
  2. {
  3. use Notifiable;
  4. use psCMS\Presenters\UserPresenter;
  5.  
  6. public static $roles = [];
  7.  
  8. protected $fillable = ['user_height', 'year_birth', 'company_id', 'enable', 'name', 'surname', 'email', 'email_verified_at', 'password', 'counter', 'url_address', 'isCompany', 'isMailing', 'content', 'nip1', 'business1', 'phone1', 'street1', 'number1', 'postal_code1', 'city1', 'country_id1', 'provincial_id1', 'nip2', 'business2', 'phone2', 'street2', 'number2', 'postal_code2', 'city2', 'country_id2', 'provincial_id2', 'nip3', 'business3', 'phone3', 'street3', 'number3', 'postal_code3', 'city3', 'country_id3', 'provincial_id3', 'cash', 'lng', 'lat', 'enable_map', 'remember_token', 'created_at', 'updated_at', 'last_login_at', 'last_login_ip'];
  9.  
  10. protected $hidden = [
  11. 'password', 'remember_token',
  12. ];
  13.  
  14.  
  15. public function roles()
  16. {
  17. return $this->belongsToMany('App\Role');
  18. }
  19.  
  20. public function mainRole()
  21. {
  22. return $this->hasOne('App\Role');
  23. }
  24.  
  25. }
  26.  
  27.  
  28.  
  29. Schema::create('users', function (Blueprint $table) {
  30. $table->bigIncrements('id');
  31. $table->bigInteger('company_id')->unsigned();
  32. $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
  33. $table->boolean('enable')->default(0);
  34. $table->string('name', 120)->nullable();
  35. $table->string('surname', 120)->nullable();
  36. $table->string('email', 120)->unique();
  37. $table->timestamp('email_verified_at')->nullable();
  38. $table->string('password');
  39. $table->bigInteger('counter')->default(0);
  40. $table->string('url_address', 160);
  41. $table->boolean('isCompany')->default(0);
  42. $table->boolean('isMailing')->default(0);
  43. $table->text('content')->nullable();
  44. $table->string('nip1', 12)->nullable();
  45. $table->string('business1', 120)->nullable();
  46. $table->string('phone1', 60)->nullable();
  47. $table->string('street1', 150)->nullable();
  48. $table->string('number1', 8)->nullable();
  49. $table->string('postal_code1', 12)->nullable();
  50. $table->string('city1', 100)->nullable();
  51. $table->bigInteger('country_id1')->default(0);
  52. $table->bigInteger('provincial_id1')->default(0);
  53. $table->string('nip2', 12)->nullable();
  54. $table->string('business2', 120)->nullable();
  55. $table->string('phone2', 60)->nullable();
  56. $table->string('street2', 150)->nullable();
  57. $table->string('number2', 8)->nullable();
  58. $table->string('postal_code2', 12)->nullable();
  59. $table->string('city2', 100)->nullable();
  60. $table->bigInteger('country_id2')->default(0);
  61. $table->bigInteger('provincial_id2')->default(0);
  62. $table->string('nip3', 12)->nullable();
  63. $table->string('business3', 120)->nullable();
  64. $table->string('phone3', 60)->nullable();
  65. $table->string('street3', 150)->nullable();
  66. $table->string('number3', 8)->nullable();
  67. $table->string('postal_code3', 12)->nullable();
  68. $table->string('city3', 100)->nullable();
  69. $table->bigInteger('country_id3')->default(0);
  70. $table->bigInteger('provincial_id3')->default(0);
  71. $table->decimal('cash', 9, 2)->default(0);
  72. $table->decimal('lng', 10, 8)->default(0);
  73. $table->decimal('lat', 10, 8)->default(0);
  74. $table->boolean('enable_map')->default(0);
  75. $table->integer('year_birth')->default(date("Y"));
  76. $table->integer('user_height')->default(0);
  77. $table->rememberToken();
  78. $table->timestamps();
  79. $table->engine = "InnoDB";
  80. });


Services:
  1. class UsersServices extends Model
  2. {
  3. protected $quarded = [];
  4. protected $fillable = ['name', 'type', 'enable'];
  5. public $timestamps = false;
  6. }
  7.  
  8. Schema::create('users_services', function (Blueprint $table) {
  9. $table->bigIncrements('id');
  10. $table->string('name', 100);
  11. $table->integer('type');
  12. $table->boolean('enable')->default(0);
  13. $table->engine = "InnoDB";
  14. });
  15.  


WYbrane/świadczone przez użytkownika usługi:

  1. Schema::create('users_services_selected', function (Blueprint $table) {
  2. $table->bigInteger('user_id')->unsigned();
  3. $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
  4. $table->bigInteger('service_id')->unsigned();
  5. $table->foreign('service_id')->references('id')->on('users_services')->onDelete('cascade');
  6. $table->engine = "InnoDB";
  7. });
  8.  
  9. class UserServicesSelected extends Model
  10. {
  11. protected $quarded = [];
  12. protected $fillable = ['user_id', 'service_id'];
  13. protected $table = 'users_services_selected';
  14. public $timestamps = false;
  15. }
  16.  


Moja aktualna funkcja do wyświetlania użytkowników



  1. public function getUserList(string $query, string $sortColumn, string $sortMethod)
  2. {
  3. if ($query != "" || $sortMethod !="") { echo "$sortColumn, $sortMethod";
  4. return User::ofRoleType(['user', 'userPremium', 'userSponsor'])
  5. ->where(function ($q) use ($query, $sortColumn, $sortMethod) {
  6. $q->where('account_paid_for', '>=', date("Y-m-d"))
  7. ->where('enable', '=', 1)
  8. ->where('email_verified_at', '<>', null)
  9. ->orderBy($sortColumn, $sortMethod);
  10. })->orderBy($sortColumn, $sortMethod)->paginate(15);
  11. } else {
  12. return User::ofRoleType(['user', 'userPremium', 'userSponsor'])
  13. ->where('account_paid_for', '>=', date("Y-m-d"))
  14. ->where('enable', '=', 1)
  15. ->where('email_verified_at', '<>', null)
  16. ->orderBy(DB::raw('IF(premium_for > CURDATE(), 0, 1)'))
  17. ->orderBy('hits', 'DESC')
  18. ->paginate(15);
  19.  
  20. }
  21. }
  22.  


Formatka do wyszukiwania:

  1. <form method="get" action="{{ route('result') }}">
  2. Wiek od <input type="text" name="s_year_birth_from" value=""></div>
  3. Wiek do <input type="text" name="s_year_birth_to" value=""></div>
  4.  
  5.  
  6. Wzrost użytkownika:
  7. Pokaż wszystko: <input type="checkbox" value="0" name="s_height[]">
  8. 150 cm: <input type="checkbox" value="1" name="s_height[]">
  9. 151-160 cm: <input type="checkbox" value="2" name="s_height[]">
  10. 161-170 cm: <input type="checkbox" value="3" name="s_height[]">
  11. 171-180 cm: <input type="checkbox" value="4" name="s_height[]">
  12. >180 cm: <input type="checkbox" value="5" name="s_height[]">
  13.  
  14.  
  15. Usługi:
  16. Pokaż wszystko: <input type="checkbox" value="0" name="s_services1[]">
  17. @foreach($services as $service)
  18. {{ $service->name }} <input type="checkbox" value="{{ $service->id }}" name="s_services1[]">
  19. @endforeach
  20.  
  21. <button type="submit">Szukaj</button>
  22. </form>
  23.  




Wzrost użytkownika = User->user_height

Data urodzenia użytkownika = User->year_birth


Potrzebuję dorobić nowe opcje wyszukiwania:

1. Wyświetlanie użytkowników posiadających odpowiedni wiek w oparciu o: s_year_birth_from i s_year_birth_to (User-> year_birth)

2. Wyświetlanie użytkowników oferujących zaznaczone w wyszukiwarce usługi (zaznaczone / oferowane usługi = UserServicesSelected)

3. Wyświetlanie użytkowników posiadających odpowiedni wzrost w oparciu o: s_height (User->user_height)


Jak można to zrobić? Chciałbym to dodać to do mojej obecnej funkcji wyświetlającej użytkowników: getUserList


Bardzo proszę o pomoc.

Go to the top of the page
+Quote Post
markonix
post 12.06.2019, 14:08:25
Post #2





Grupa: Zarejestrowani
Postów: 2 707
Pomógł: 290
Dołączył: 16.12.2008
Skąd: Śląsk

Ostrzeżenie: (0%)
-----


nip1, nip2, nip3.. Hmmm.. Może jeszcze nip4, i nip5.. Zwracam uwagę też, że nip to po angielsku co najwyżej może się kojarzyć z sutkiem..

Klasa typu service, w której jest kod migracji i parametry z modelu? O co tu chodzi?

Data urodzenia, w której ma być domyślnie rok utworzenia tabeli? Poza tym nie lepiej datę?

Wzrost domyślnie wynosi 0?

CamelCase i under score w jednej tabeli.

bigInteger dla kraju? Serio mamy 4 biliony Państw na ziemi?

engine jest domyślnie innodb, klucze obce warto wydzielić wizualnie pod deklarację tabeli, lepiej się czyta.

W funkcjach jakieś echo.


--------------------
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 09:54