Я новичок в Laravel. Я делаю функцию для отображения списка продуктов с фильтром.
У меня небольшая проблема с моим кодом
У меня есть этот код:
class Product extends Model
{
use ScopeActiveTrait;
use Slugable;
public function setNameAttribute($value)
{
$this->attributes['name'] = $value;
$this->attributes['slug'] = $this->makeSlug($value);
}
protected $fillable = ['delivery_time', 'product_type', 'name', 'title', 'description', 'keywords', 'content', 'vat_id', 'main_category_id', 'enable', 'slug', 'small_description'];
protected $quarded = ['id'];
public $timestamps = false;
public function vat()
{
return $this->belongsTo('App\Models\VAT', 'vat_id');
}
public function category()
{
return $this->belongsTo('App\Models\Category', 'main_category_id');
}
public function selectedCategory()
{
return $this->hasMany('App\Models\SelectedProductCategory', 'product_id', 'id');
}
public function related()
{
return $this->belongsTo('App\Models\RelatedProduct');
}
public function features()
{
return $this->hasMany('App\Models\SelectedProductFeature');
}
public function frontImage()
{
return $this->hasMany('App\Models\UploadImage', 'file_id', 'id')->orderBy('order', 'ASC')->where('file_type', 'products');
}
}
public function getDyamicProducts(int $filterDrawer, int $filterMounting, int $filtershelfs, $categories, string $query)
{
if ($query != "") {
$query = $this->model->select('name', 'slug', 'products.id', 'small_description', 'promo_desc')
->with(['frontImage', 'selectedCategory', 'features'])
->where('title', 'like', '%' . $query . '%')
->orWhere('name', 'like', '%' . $query . '%')
->orWhere('content', 'like', '%' . $query . '%')
->active()
->orderBy('name', 'asc');
} else {
$query = $this->model->select('name', 'slug', 'products.id', 'small_description', 'promo_desc')
->with(['frontImage', 'selectedCategory', 'features'])
->whereHas('selectedCategory', function ($q) use ($categories) {
$q->whereIn('category_id', $categories);
})->active()
->orderBy('name', 'asc');
}
if ($filterDrawer != 0) {
$query->whereHas('features', function ($query, $filterDrawer) {
$query->where('key', 'number_drawers');
$query->where('description', $filterDrawer);
});
}
if ($filterMounting == 1) {
$query->whereHas('features', function ($query) {
$query->where('key', 'form-2');
$query->where('description', 1);
});
}
if ($filterMounting == 2) {
$query->whereHas('features', function ($query) {
$query->where('key', 'form-3');
$query->where('description', 1);
});
}
if ($filtershelfs != 0) {
$query->whereHas('features', function ($query, $filterDrawer) {
$query->where('key', 'number_shelves');
$query->where('description', $filterDrawer);
});
}
return $query->get();;
}
class SelectedProductFeature extends Model
{
protected $fillable = ['product_id', 'feature_id', 'description', 'key'];
protected $quarded = ['id'];
}
class SelectedProductCategory extends Model
{
protected $fillable = ['product_id', 'category_id'];
protected $quarded = ['id'];
}
Schema::create('selected_product_features', function (Blueprint $table) {
$table->id();
$table->bigInteger('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->bigInteger('feature_id')->unsigned();
$table->foreign('feature_id')->references('id')->on('product_features')->onDelete('cascade');
$table->string('key', 50);
$table->text('description')->nullable();;
$table->timestamps();
});
Schema::create('selected_product_categories', function (Blueprint $table) {
$table->id();
$table->bigInteger('product_id')->unsigned()->default(0);
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->bigInteger('category_id')->unsigned()->default(0);
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->timestamps();
});
Когда я запускаю функцию: getDyamicProducts
У меня ошибка:
Too few arguments to function App\Repositories\ProductRepository::App\Repositories\{closure}(), 1 passed in /Applications/XAMPP/xamppfiles/htdocs/roelle/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php on line 988 and exactly 2 expected
Эта ошибка находится в: $ query-> whereHas ('features', function ($ query, $ filterDrawer) {
Что не так?
Как исправить эту ошибку?