orWhereHas not existing laravel Eloquent php 7.4

309 views Asked by At

Here is my Macro for relation search in Laravel Eloquent. Bellow is my Macro in AppServiceProvider.php

    Builder::macro('whereLike', function ($attributes, string $searchTerm) {
        $this->where(function (Builder $query) use ($attributes, $searchTerm) {
            foreach (Arr::wrap($attributes) as $attribute) {
                $query->when(
                    str_contains($attribute, '.'),
                    function (Builder $query) use ($attribute, $searchTerm) {
                        [$relationName, $relationAttribute] = explode('.', $attribute);

                        $query->orWhereHas($relationName, function (Builder $query) use 
                    ($relationAttribute, $searchTerm) {
                            $query->where($relationAttribute, 'LIKE', "%{$searchTerm}%");
                        });
                    },
                    function (Builder $query) use ($attribute, $searchTerm) {
                        $query->orWhere($attribute, 'LIKE', "%{$searchTerm}%");
                    }
                );
            }
        });

        return $this;
    });

Bellow is my search Query in MemberController.php

 public function getMembers(request $request){

 $members = Member::with('products')->where('groupId', 1)->whereLike(['firstname', 
   'lastname', 'products.name'], $request->searchValue)->paginate(10);
dd($members);
}

The "whereLike" is invoked from the appServiceProvider.php as a macro in there thus where the error occur if i remove the members.name the macro executes perfectly.

I get an Error with orWhereHas

"Call to undefined method Illuminate\Database\Query\Builder::orWhereHas()"

0

There are 0 answers