尝试按关系筛选时出现错误



我是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('AppModelsVAT', 'vat_id');
}
public function category()
{
return $this->belongsTo('AppModelsCategory', 'main_category_id');
}
public function selectedCategory()
{
return $this->hasMany('AppModelsSelectedProductCategory', 'product_id', 'id');
}

public function related()
{
return $this->belongsTo('AppModelsRelatedProduct');
}
public function features()
{
return $this->hasMany('AppModelsSelectedProductFeature');
}

public function frontImage()
{
return $this->hasMany('AppModelsUploadImage', '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 AppRepositoriesProductRepository::AppRepositories{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({

怎么了?

如何修复此错误?

您将无法通过这种方式向匿名函数传递额外的参数,在该上下文中,$filterDrawer将是null,从而导致错误。试试这个:

$query->whereHas('features', function ($query) use ($filterDrawer) {
// code
});

最新更新