laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.44k stars 11k forks source link

orWhereHas in where closure #3346

Closed MaatwebsiteSupport closed 10 years ago

MaatwebsiteSupport commented 10 years ago

I'm trying to insert orWhereHas in a where closure to create a grouped where statement. However that results in an error message. The code I'm trying to run:

    public function scopeWhereActionIsRequired($query)
    {
        return $query->where('status_id', 2)
                        ->where(function($query) {
                                $query->orWhereRaw('`total` > `payed`');
                                $query->orWhereRaw('`payed` IS NULL');

                                $query->orWhereHas('items', function($q) {
                                        $q->whereRaw('`amount` > `supplied`');
                                });
                        });
    }

This results in the following error:

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

I'm running Laravel 4.1.18 on PHP 5.4.12

anlutro commented 10 years ago

See #3269

Jerubaalking commented 4 years ago

orWhereHas() problem


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;
        });`

I know this issue is closed but am on laravel 7.4 and still having this issue. Please advice Thank you