laravel-query-builder: Call to undefined method

Trying to migrate my project to Laravel 9. After upgrading laravel-query-builder to version 5.0.0 I get the error:

Call to undefined method Spatie\QueryBuilder\QueryBuilder::allowedAppends()

It seems like there is no more appending attributes point for version 5 as well. Why this feature was removed?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 15 (5 by maintainers)

Most upvoted comments

We felt like this feature didn’t belong in the package and it added too much complexity.

For me, the Feauture appending attributes was very helpful. For example to add computed properties only when needed or allowed by role.

Fortunately you still kept all needed functions in QueryBuilderRequest. So I only had to restore AppendsAttributesToResults and InvalidAppendQuery and overwrite __call from QueryBuilder.

Is it planned to leave QueryBuilderRequest like this? If not, it would be a consideration changing variables like

private static $includesArrayValueDelimiter = ','; to protected for easier overwriting?

Good to know, maybe update the “changelog” and release notes? Thank you

@TheFrankman This is how I solved it for now. I’ll update this comment if I find some conflicts in my app. But it seems to work fine for now, with plain ->get() aswell as ->paginate().

if ($request->append) {
   $appends = collect(explode(',', $request->append));
   $data->each(function ($row) use ($appends) {
      $appends->each(function ($attr) use ($row) {
         $attrFnName = Str::camel('get_' . $attr . '_attribute');
         if (method_exists($row, $attrFnName)) {
            return $row->append($attr);
         }
         
         throw new \Exception("Append attribute <$attr> not found in model " . get_class($row));
      });
      
      return $row;
   });
}

Updated more complex API function:

protected function addAppendsFix(Request $request, Collection|LengthAwarePaginator|Model $data): Collection|LengthAwarePaginator|Model {
   if ($request->append) {
      $appends = collect(explode(',', $request->append));

      $callAppendFn = function ($row, $appends) {
         $appends->each(function ($attr) use ($row) {
            $attrFnName = Str::camel('get_' . $attr . '_attribute');
            if (method_exists($row, $attrFnName)) {
               return $row->append($attr);
            }

            throw new \Exception("Append attribute <$attr> not found in model " . get_class($row));
         });

         return $row;
      };

      if ($data instanceof \Illuminate\Pagination\LengthAwarePaginator || $data instanceof \Illuminate\Database\Eloquent\Collection) {
         $data->each(fn($row) => $callAppendFn($row, $appends));
      } else if ($data instanceof \Illuminate\Database\Eloquent\Model) {
         $callAppendFn($data, $appends);
      } else {
         throw new \Exception("Available append types didn't match for" . gettype($data));
      }
   }

   return $data;
}

// ...

$data = $this->addAppendsFix($request, $data);

That is a great pity that the functionality has been removed. This is important for me in some respects. Now I’ll probably have to work with not so nice workarounds.

@kurorido how did you install the fork? Composer wouldn’t allow me to install it, because the branch name "Invalid version string "main"

Update: Could solve it using

   "repositories": [
      {
         "type": "vcs",
         "url": "https://github.com/kurorido/laravel-query-builder.git"
      }
   ],

and "spatie/laravel-query-builder": "dev-restore_append"