Mam mały problem z Laravelem.
Mam taki kod:
Schema::create('dish_values', function (Blueprint $table) { $table->bigIncrements('id'); $table->bigInteger('dishes_id')->unsigned(); $table->foreign('dishes_id')->references('id')->on('dishes')->onDelete('cascade'); $table->bigInteger('food_ingredient_id')->unsigned(); $table->foreign('food_ingredient_id')->references('id')->on('food_ingredients'); $table->bigInteger('company_id')->unsigned(); $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); $table->decimal('quantity', 9, 2)->default(0); $table->engine = "InnoDB"; $table->charset = 'utf8mb4'; $table->collation = 'utf8mb4_unicode_ci'; }); Schema::create('dishes', function (Blueprint $table) { $table->bigIncrements('id'); $table->bigInteger('company_id')->unsigned(); $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); $table->string('name', 100)->nullable(); $table->longText('description')->nullable(); $table->char('enable', 1)->default(1); $table->char('allergen', 1)->default(0); $table->engine = "InnoDB"; $table->charset = 'utf8mb4'; $table->collation = 'utf8mb4_unicode_ci'; }); Schema::create('food_ingredients', function (Blueprint $table) { $table->bigIncrements('id'); $table->bigInteger('company_id')->unsigned(); $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); $table->string('name', 120)->nullable(); $table->decimal('garbage', 9, 2)->default(0); $table->decimal('energy_value', 9, 2)->default(0); $table->decimal('protein', 9, 2)->default(0); $table->decimal('fat', 9, 2)->default(0); $table->decimal('available_carbohydrates', 9, 2)->default(0); $table->decimal('roughage', 9, 2)->default(0); $table->longText('description')->nullable(); $table->string('url_address', 160); $table->char('allergen', 1)->default(0); $table->char('allergen1', 1)->default(0); $table->char('allergen2', 1)->default(0); $table->char('allergen3', 1)->default(0); $table->char('available_in_demo', 1)->default(0); $table->char('enable', 1)->default(1); $table->engine = "InnoDB"; $table->charset = 'utf8mb4'; $table->collation = 'utf8mb4_unicode_ci'; }); class Dish extends Model { protected $quarded = ['id']; protected $fillable = ['company_id', 'name', 'description', 'enable', 'allergen']; public $timestamps = false; public function components() { return $this->hasManyThrough('App\DishValues', 'App\Dish', 'id', 'dishes_id'); } public function foodIngredient() { return $this->hasManyThrough('App\FoodIngredient', 'App\DishValues', 'dishes_id', 'id'); } public function withoutAllergen() { return $this->foodIngredient()->where('allergen1', '=', '1'); } }
Mam funkcję do wyświetlenia danych:
Dish::where('enable', '=', 1)->with('components') ->where(function($q)use ($companyId) { $q->where('company_id', $companyId) ->orWhere('company_id', 1); }) ->where([ ['name', 'LIKE', '%' . $query . '%'], ['enable', '=', 1], ])->get()
Powyższy kod działa poprawnie.
Chciałbym filtrować po alergenach:
Dish::where('enable', '=', 1)->with('components') ->where(function($q)use ($companyId) { $q->where('company_id', $companyId)->where('allergen1', 1) ->orWhere('company_id', 1); }) ->where([ ['name', 'LIKE', '%' . $query . '%'], ['enable', '=', 1], ])->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ć?