Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leftJoinSub() is not a method in Laravel<5.6 #171

Closed
pdanny opened this issue Jul 14, 2018 · 12 comments · Fixed by suenert/laravel-scout-tntsearch-driver#2 or #189
Closed

leftJoinSub() is not a method in Laravel<5.6 #171

pdanny opened this issue Jul 14, 2018 · 12 comments · Fixed by suenert/laravel-scout-tntsearch-driver#2 or #189

Comments

@pdanny
Copy link

pdanny commented Jul 14, 2018

Trying to apply pagination on a query with where clauses results in an error for L5.5 or older. leftJoinSub() is only a method in L5.6.

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

@MehediDracula
Copy link
Contributor

I'm also facing the same issue on Laravel 5.5. Looks like this is happening because of this commit a900b0a

I've downgraded to version 3.0.6 to fix this issue.

@hackjie
Copy link

hackjie commented Aug 2, 2018

@nticaric Laravel 5.5 face the same problem

 Topic::search($q)->paginate(15);

@thoresuenert
Copy link

We will take care of this.
We are working on a replacement for leftJoinSub introduced in L5.6

@thoresuenert
Copy link

thoresuenert commented Aug 2, 2018

Can anyone test this fix:

replace leftJoinSub https://github.com/teamtnt/laravel-scout-tntsearch-driver/blob/master/src/Engines/TNTSearchEngine.php#L141 with:

->join('('.$sub->getQuery()->toSql().') as sub', $subQualifiedKeyName, '=', $qualifiedKeyName, 'left')
    ->addBinding($sub->getQuery()->getBindings(), 'join')

@michaelklopf
Copy link
Contributor

Fix didn't work. Will work on it later this week.

@la-jamesh
Copy link

la-jamesh commented Aug 11, 2018

Running into this as well when using Model::paginate()

@Mohammad-Alavi
Copy link

I am also running into this problem. any news of a fix soon?

@michaelklopf
Copy link
Contributor

How about this:

Replace the ->leftJoinSub line in

https://github.com/teamtnt/laravel-scout-tntsearch-driver/blob/master/src/Engines/TNTSearchEngine.php#L141

with

->leftJoin(DB::raw('(' . $sub->getQuery()->toSql() .') as sub'), $subQualifiedKeyName, '=', $qualifiedKeyName)

And on top add:
use Illuminate\Support\Facades\DB;

@luisdalmolin
Copy link

Getting the same error in a Laravel 5.5 app. I was able to fix by adding the missing methods as macros in my AppServiceProvider:

use Closure;
use InvalidArgumentException;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;

// needed because of the TNT search scout package uses methods that are specific from laravel 5.6
// may be able to remove this if this app gets upgraded OR they fix the issue: https://github.com/teamtnt/laravel-scout-tntsearch-driver/issues/171
QueryBuilder::macro('joinSub', function ($query, $as, $first, $operator = null, $second = null, $type = 'inner', $where = false) {
    list($query, $bindings) = $this->createSub($query);
    $expression = '('.$query.') as '.$this->grammar->wrap($as);
    $this->addBinding($bindings, 'join');
    return $this->join(new Expression($expression), $first, $operator, $second, $type, $where);
});

QueryBuilder::macro('leftJoinSub', function ($query, $as, $first, $operator = null, $second = null) {
    return $this->joinSub($query, $as, $first, $operator, $second, 'left');
});

QueryBuilder::macro('createSub', function ($query) {
    if ($query instanceof Closure) {
        $callback = $query;
        $callback($query = $this->forSubQuery());
    }
    return $this->parseSub($query);
});

QueryBuilder::macro('parseSub', function ($query) {
    if ($query instanceof self || $query instanceof EloquentBuilder) {
        return [$query->toSql(), $query->getBindings()];
    } elseif (is_string($query)) {
        return [$query, []];
    } else {
        throw new InvalidArgumentException;
    }
});

@thoresuenert
Copy link

@nticaric how to solve this issue?
we can use the same code an put the macros from @luisdalmolin into a version which supports l5.5 and release a new version with support >l5.6 ?

@IhwanID
Copy link

IhwanID commented Sep 2, 2018

got this : BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::leftJoinSub()

when i user method $posts = Post::search($request->get('query'))->where('status' , 1)->orderBy('published_at', 'DESC')->paginate(4);

but working smoothly when just use $posts = Post::search($request->get('query'))->get();

i dunno why, help me. thank you

@michaelklopf
Copy link
Contributor

See #189 for a possible fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
9 participants