Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Problem z wyszukiwaniem w Laravel Equivalent
Forum PHP.pl > Forum > Przedszkole
trifek
Witajcie,
Mam mały problem z wyszukiwaniem w Laraverze. Projekt piszę w Laravel 8.

Mam takie modele / migracje:

  1. class Category extends Model
  2. {
  3. use ScopeActiveTrait,
  4. NodeTrait,
  5. HasSlug,
  6. SoftDeletes;
  7.  
  8. protected $guarded = ['id'];
  9.  
  10. protected $fillable = [
  11. 'category_name',
  12. 'description',
  13. 'keywords',
  14. 'content',
  15. 'enable',
  16. 'photo',
  17. 'order',
  18. 'slug',
  19. '_lft',
  20. '_rgt',
  21. 'parent_id',
  22. 'value'
  23. ];
  24.  
  25. public $timestamps = false;
  26.  
  27. protected $dates = [
  28. 'deleted_at'
  29. ];
  30.  
  31. protected $casts = [
  32. 'order' => 'integer',
  33. 'enable'=>'boolean'
  34. ];
  35.  
  36. /**
  37.   * Get the options for generating the slug.
  38.   */
  39. public function getSlugOptions() : SlugOptions
  40. {
  41. return SlugOptions::create()
  42. ->generateSlugsFrom('category_name')
  43. ->slugsShouldBeNoLongerThan(160)
  44. ->saveSlugsTo('slug');
  45. }
  46.  
  47. }
  48.  
  49. public function up()
  50. {
  51. Schema::table('categories', function (Blueprint $table) {
  52. $table->text('value')->after('parent_id')->nullable();
  53. });
  54. }
  55.  
  56. Schema::create('categories', function (Blueprint $table) {
  57. $table->id();
  58. $table->string('category_name', 155);
  59. $table->string('description', 155)->nullable();
  60. $table->string('keywords', 155)->nullable();
  61. $table->longText('content')->nullable();
  62. $table->boolean('enable')->default(false);
  63. $table->string('photo', 155)->nullable();
  64. $table->bigInteger('order')->default(0);
  65. $table->string('slug', 160)->nullable();
  66. $table->softDeletes();
  67. NestedSet::columns($table);
  68. $table->engine = "InnoDB";
  69. $table->charset = 'utf8mb4';
  70. $table->collation = 'utf8mb4_unicode_ci';
  71. });




Używam komponentu: kalnoy/nestedset - ale tutaj nie jest to istotne.

W kolumnie categories.value mam np. taką wartość:


AUDI - TT - 8N (1998-2006) - UKŁAD WYDECHOWY - ELEMENTY MOCUJĄCE


Kiedy próbuję wyszukać za pomocą funkcji:
  1.  
  2. public function getSelect2(Category $category, Request $request)
  3. {
  4. if ($request->search != '' && strlen($request->search) >= 4) {
  5. $categories = Category::select('id', 'value')
  6. ->where(function ($query) use ($request) {
  7. $query->orWhere('category_name', 'like', '%' . $request->search . '%');
  8. $query->orWhere('description', 'like', '%' . $request->search . '%');
  9. $query->orWhere('keywords', 'like', '%' . $request->search . '%');
  10. $query->orWhere('content', 'like', '%' . $request->search . '%');
  11. $query->orWhere('value', 'like', '%' . $request->search . '%');
  12. })->active()
  13. ->orderby('order', 'asc')
  14. ->get();
  15. $response = array();
  16. foreach ($categories as $item) {
  17. $response[] = array(
  18. "id" => $item->id,
  19. "text" => $item->value
  20. );
  21. }
  22. return $response;
  23. }
  24. }



Np. : Audi - mam dużo wyników.
Kiedy wpiszę: 'audi układ' - nie mam żadnego wyniku sad.gif


Jak to naprawić?
SmokAnalog
WHERE `kolumna` LIKE '%audi układ%' szuka rekordów, które mają dokładnie taki tekst wewnątrz kolumny, czyli "audi układ". Musiałbyś albo rozbić to na słowa ("audi" i "układ") i szukać osobno dla każdego słowa i każdej kolumny: WHERE `kolumna` LIKE '%audi%' OR `kolumna` LIKE `%układ%` (lub AND) albo bawić się w MATCH ... AGAINST.

Ale zamiast tego polecam poczytać dokumentację Laravel Scout.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.