Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]Filtrowanie wyników w Laravel equivalent relationship
trifek
post 15.02.2020, 16:33:12
Post #1





Grupa: Zarejestrowani
Postów: 340
Pomógł: 0
Dołączył: 28.09.2015

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


Witam serdecznie.
Mam mały problem z Laravelem.

Mam taki kod:
  1. Schema::create('dish_values', function (Blueprint $table) {
  2. $table->bigIncrements('id');
  3. $table->bigInteger('dishes_id')->unsigned();
  4. $table->foreign('dishes_id')->references('id')->on('dishes')->onDelete('cascade');
  5. $table->bigInteger('food_ingredient_id')->unsigned();
  6. $table->foreign('food_ingredient_id')->references('id')->on('food_ingredients');
  7. $table->bigInteger('company_id')->unsigned();
  8. $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
  9. $table->decimal('quantity', 9, 2)->default(0);
  10. $table->engine = "InnoDB";
  11. $table->charset = 'utf8mb4';
  12. $table->collation = 'utf8mb4_unicode_ci';
  13. });
  14.  
  15.  
  16. Schema::create('dishes', function (Blueprint $table) {
  17. $table->bigIncrements('id');
  18. $table->bigInteger('company_id')->unsigned();
  19. $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
  20. $table->string('name', 100)->nullable();
  21. $table->longText('description')->nullable();
  22. $table->char('enable', 1)->default(1);
  23. $table->char('allergen', 1)->default(0);
  24. $table->engine = "InnoDB";
  25. $table->charset = 'utf8mb4';
  26. $table->collation = 'utf8mb4_unicode_ci';
  27. });
  28.  
  29. Schema::create('food_ingredients', 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->string('name', 120)->nullable();
  34. $table->decimal('garbage', 9, 2)->default(0);
  35. $table->decimal('energy_value', 9, 2)->default(0);
  36. $table->decimal('protein', 9, 2)->default(0);
  37. $table->decimal('fat', 9, 2)->default(0);
  38. $table->decimal('available_carbohydrates', 9, 2)->default(0);
  39. $table->decimal('roughage', 9, 2)->default(0);
  40. $table->longText('description')->nullable();
  41. $table->string('url_address', 160);
  42. $table->char('allergen', 1)->default(0);
  43. $table->char('allergen1', 1)->default(0);
  44. $table->char('allergen2', 1)->default(0);
  45. $table->char('allergen3', 1)->default(0);
  46. $table->char('available_in_demo', 1)->default(0);
  47. $table->char('enable', 1)->default(1);
  48. $table->engine = "InnoDB";
  49. $table->charset = 'utf8mb4';
  50. $table->collation = 'utf8mb4_unicode_ci';
  51. });
  52.  
  53.  
  54.  
  55. class Dish extends Model
  56. {
  57. protected $quarded = ['id'];
  58. protected $fillable = ['company_id', 'name', 'description', 'enable', 'allergen'];
  59. public $timestamps = false;
  60.  
  61. public function components()
  62. {
  63. return $this->hasManyThrough('App\DishValues', 'App\Dish', 'id', 'dishes_id');
  64. }
  65.  
  66. public function foodIngredient()
  67. {
  68. return $this->hasManyThrough('App\FoodIngredient', 'App\DishValues', 'dishes_id', 'id');
  69. }
  70.  
  71. public function withoutAllergen()
  72. {
  73. return $this->foodIngredient()->where('allergen1', '=', '1');
  74. }
  75. }



Mam funkcję do wyświetlenia danych:
  1. Dish::where('enable', '=', 1)->with('components')
  2. ->where(function($q)use ($companyId) {
  3. $q->where('company_id', $companyId)
  4. ->orWhere('company_id', 1);
  5. })
  6. ->where([
  7. ['name', 'LIKE', '%' . $query . '%'],
  8. ['enable', '=', 1],
  9. ])->get()


Powyższy kod działa poprawnie.
Chciałbym filtrować po alergenach:
  1. Dish::where('enable', '=', 1)->with('components')
  2. ->where(function($q)use ($companyId) {
  3. $q->where('company_id', $companyId)->where('allergen1', 1)
  4. ->orWhere('company_id', 1);
  5. })
  6. ->where([
  7. ['name', 'LIKE', '%' . $query . '%'],
  8. ['enable', '=', 1],
  9. ])->get()


Jednak otrzymuje taki błąd:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'allergen1' in 'where clause' (SQL: select count(*) as aggregate from dishes where enable = 1 and allergen1 = 1 and (name LIKE %% and enable = 1 and enable = 1))

Wie ktoś może jak to naprawić?
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: 29.03.2024 - 14:21