当我尝试按关系过滤时出现错误问题

Problem with error when I trying filter by relation

我是 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) {

怎么了?

我该如何修复这个错误?

您将无法通过这种方式将额外参数传递给匿名函数,$filterDrawer 在该上下文中将是 null - 因此会出现错误。试试这个:

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