0

I am trying to get the data from group model but i am relate the student table new_group_id to group table id if it not null else connect the group_id to group table id.

public function grade(Builder $query)
{
    return $query
        ->when($this->new_group_id != 0, function ($q) {
            return $q->with('new_group_id');
        })
        ->when($this->new_group_id === 0, function ($q) {
            return $q->with('group_id');
        });
}

But given the error is

Argument 1 passed to App\Students::grade() must be an instance of App\Builder

please help me to solve these error

ParisaN
  • 1,816
  • 2
  • 23
  • 55

1 Answers1

0

It looks as though you're using a query scope which Laravel will resolve and inject an instance of Builder into your function via the service container.

For this to happen though you need to follow the Laravel convention of prefixing your local scope with the word scope:

public function scopeGrade($query)
{
    return $query
      ->when($this->new_group_id != 0,function($q){
          return $q->with('new_group_id');
      })
      ->when($this->new_group_id === 0,function($q){
          return $q->with('group_id');
      });
}
Peppermintology
  • 9,343
  • 3
  • 27
  • 51